Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Varying error handling throughout Osprey #165

Open
dploeger opened this issue Aug 2, 2018 · 1 comment
Open

Varying error handling throughout Osprey #165

dploeger opened this issue Aug 2, 2018 · 1 comment
Assignees

Comments

@dploeger
Copy link

dploeger commented Aug 2, 2018

Hmm... maybe I simply don't get it, but why are errors handled differently throughout the osprey-method-handler, osprey-router and osprey-server?

I would believe that osprey would format the error messages according in that way, that its own error handler could format them, but this only seems to be the case in osprey-method-handler when it's calling createValidationError. Even there, some errors (like unexpected content type) are not eligible for the osprey error handler, because they use the "createError" function without adding errors to the requestErrors array, that the error handler requires.

Is that on purpose? Because, currently, errors like an invalid path according to the RAML specification are rendered as an HTML page (because of Express' own error handler) for requests specifying Accept: application/json...

@dploeger
Copy link
Author

dploeger commented Aug 4, 2018

For the time being, I've created the following wrapper to catch all nonconforming errors:

import { errorHandler } from 'osprey'

export function ospreyErrorWrapper(error, req, res, next): void {
  if (!error || error.requestErrors) {
    next(error)
  } else {
    let newError = errorHandler.createError(
      [
        {
          type: error.name,
          keyword: error.name,
          message: error.message
        }
      ],
      error.status
    )
    newError.stack = error.stack
    errorHandler()(newError, req, res, next)
  }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants