claudiajs / claudia

Deploy Node.js projects to AWS Lambda and API Gateway easily

Home Page:https://claudiajs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Create/update gives ConflictException at "REST API apigateway.setAcceptHeader"

prajankya opened this issue · comments

updating REST API apigateway.setAcceptHeader
ConflictException: Another resource with the same parent already has this name

I have read #185 and #184, the issue is same, but no solution.

I tried with claudia@5.5
aws-sdk@2.454.0

I even went into local folder for claudia(I had installed it globally) and force updated aws-sdk to 2.483.0

Still with no solution.

My command

claudia create --region ap-south-1 --api-module app --set-env-from-json claudia_env_variables.json  --policies policies  --configure-db

Or

claudia update --set-env-from-json claudia_env_variables.json --policies policies --configure-db

both are giving the same problem.

Following is my part of code:

app.js

const ApiBuilder = require("claudia-api-builder")
const api = new ApiBuilder();

require('./v1/users')(api, '/api/v1/users')

module.exports = api;

./v1/users.js

'use strict';
module.exports = function (api, base) {
  api.get(base + '/', function (req) {
    //function
    return new ApiBuilder.ApiResponse(result, {
      'Content-Type': 'application/json'
    }, 200);
  });

  api.post(base + '/', function (req) {
   //...
  })
}

**One thing I have noticed is I can define only one endpoint (only either get/post/put) in the users file, If I comment the api.post as shown above, all will work and get deployed.(I checked and verified, it is either of get or post or put or anything, but only one works.) **

Exact Error response

REST API	apigateway.setAcceptHeader
ConflictException: Another resource with the same parent already has this name: users
    at Object.extractError (/usr/local/lib/node_modules/claudia/node_modules/aws-sdk/lib/protocol/json.js:51:27)
    at Request.extractError (/usr/local/lib/node_modules/claudia/node_modules/aws-sdk/lib/protocol/rest_json.js:55:8)
    at Request.callListeners (/usr/local/lib/node_modules/claudia/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
    at Request.emit (/usr/local/lib/node_modules/claudia/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
    at Request.emit (/usr/local/lib/node_modules/claudia/node_modules/aws-sdk/lib/request.js:683:14)
    at Request.transition (/usr/local/lib/node_modules/claudia/node_modules/aws-sdk/lib/request.js:22:10)
    at AcceptorStateMachine.runTo (/usr/local/lib/node_modules/claudia/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at /usr/local/lib/node_modules/claudia/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (/usr/local/lib/node_modules/claudia/node_modules/aws-sdk/lib/request.js:38:9)
    at Request.<anonymous> (/usr/local/lib/node_modules/claudia/node_modules/aws-sdk/lib/request.js:685:12) {
  message: 'Another resource with the same parent already has this name: users',
  code: 'ConflictException',
  time: 2019-06-27T04:21:59.962Z,
  requestId: '1b6eeea7-9893-11e9-b8b6-d13b5e23cac1',
  statusCode: 409,
  retryable: false,
  retryDelay: 12.669249980494879
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1

So am I doing anything wrong, or it is a problem with API builder or aws-sdk?

Hi - I just tried a copy of the code you provided in the issue, and a clean project with two handlers (get/put) on the same resource path, and both seem to work.

the problem is somewhere else in the code, not in the part you pasted.

can you perhaps create a full minimal working example that fails for you and post it to github, so I can be sure to working from the same code as you are when troubleshooting.

Hey gojko, I tried with cleaner code after you told, and I found the solution,

api.get(base + '/', function (req) {

The last / in the end is causing the issue, what I had done previously was I had created post,get,put delete all four endpoints with / at the end, which is I guess tried to "addHeaders" for same CORS (options) four times for / path.

Now after removing the trailing slash, all are getting registered, and I am getting separate options.

I was unaware that trailing / will cause issues.

Am I correct on this one? or am I missing something which is my code? (I haven't changed a dime except removing the slash)

If you found the same about removing trailing slash, can you please document it?

Thanks.

we generally use API endpoints that are not directories, but resources (so the endpoint for us is normally /users, not /users/). I don't think the trailing slash will create an issue, but this might be an API GW limitation. in any case, if the code works for you now, I'll close the issue