swagger-node-runner: Various error handling issues

Hi there, I’m having a hard time with error handling. When a validation error is raised it comes back as an ugly JSON response with my request buffer attached. How do developers opt out, or modify this error object?

I am using a project generated with swagger project create, with express.

The other thing, is that the swagger error responses are coming back clipped. Example:

{"message":"Response validation failed: failed schema validation","code":"SCHE

This may be related to #30

Am I please able to have some help in resolving these issues?

Many thanks, Wilfred

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 2
  • Comments: 18

Most upvoted comments

Is that request or response validation that you’re having issues with?

If it’s request validation, you can write your own error handling fitting (which is very much like an express middleware) to handle validation errors and customise what’s sent to the client. I a

Step 1

you’ll need to update your swagger configuration in {project_root}/config/default.yaml to override the default json_error_handler with your own. For example, change:

swagger_controllers:
      - onError: json_error_handler

To

swagger_controllers:
      - onError: errorFitting

Step 2

Create fitting errorFitting by creating {project_root}/api/fittings/errorFitting.js. To start with, you can just copy the existing json_error_handler into this and then customise it. You can detect if there was a validation error by looking at the error message like the tests does.

By the way, I saw that you were using raw res.send etc. from your code. I think you could do a lot better by abstracting that away and use something like http-errors to simplify controller code and take advantage of middleware chaining via next in express, which is also honoured by swagger-node-runner. For example, here’s how a controller function in our codebase looks like:

const createError = require('http-errors');
const createSuccess = require('http-success');
...
function register(req, res, next) {
  const user = req.swagger.params.body.value;
  authService.register(user, context, (error, result) => {
    if (error) {
      if (error instanceof ResourceError) return next(createError(error.status, error.message));
      if (error instanceof DatabaseError) return next(createError(error.status, error.message));
      return next(createError(401));
    }
    return next(null, createSuccess(200, result.data));
  });
}