Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throwing classes derived from ApiBuilder.ApiResponse is not working correctly #69

Open
CherryDT opened this issue Oct 31, 2022 · 0 comments

Comments

@CherryDT
Copy link

CherryDT commented Oct 31, 2022

  • 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant