Skip to content

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

Open
@CherryDT

Description

@CherryDT
  • 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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions