claudiajs / claudia-api-builder

Use AWS API Gateway as if it were a lightweight JavaScript web server

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Throwing classes derived from ApiBuilder.ApiResponse is not working correctly

CherryDT opened this issue · comments

  • Expected behaviour: If an error is instanceof ApiBuilder.ApiResponse, it should work the same as if it is exactly that class.

  • What actually happens: If an error class is inheriting from ApiBuilder.ApiResponse, it no longer functions like a response object but instead results in an errorMessage with a JSON representation of the error.

class NotFoundError extends ApiBuilder.ApiResponse {
  constructor (message = 'Not found') {
    super(JSON.stringify({ error: message }), { 'Content-Type': 'application/json' }, 404)
  }
}

// This doesn't work
throw new NotFoundError()

// This works
throw new ApiBuilder.ApiResponse(JSON.stringify({ error: 'Not found' }), { 'Content-Type': 'application/json' }, 404)

// This is true, so why does it not work?
console.log(new NotFoundError() instanceof ApiBuilder.ApiResponse)