apigee-127/swagger-tools

Handle path that is not in swagger definition

ahmad-zul opened this issue · 0 comments

Hi,
I am trying to handle the error response when trying to access the a path that is not in swagger definition. For example:

  1. GET: http://localhost:8080/v1/authorise/_latest -> Valid and processed.
  2. GET: http://localhost:8080/v1/authorise/_latesta -> Not valid and not exists in swagger definition.

I am trying to change the error message when users try to access a path that is no valid.
My initialize middleware is as below:

// Initialize middleware
swaggerTools.initializeMiddleware(swaggerDoc, function(middleware) {
  // Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
  app.use(middleware.swaggerMetadata());

  // Verify and decode JWT in Authorization header - upstream handlers will find decoded token in "res.bearer"
  app.use(middleware.swaggerSecurity(bearer));

  // Validate Swagger requests
  app.use(middleware.swaggerValidator({
    validateResponse: true
  }));

  // Route validated requests to appropriate controller
  app.use(middleware.swaggerRouter(options));

  // Serve the Swagger documents and Swagger UI
  app.use(middleware.swaggerUi());

  app.use(helmet());
  app.use(morgan("combined"));

  app.use(function error(err, req, res, next) {
    writer(res, res.statusCode);
  });
});

The invalid path error does not go to my error function.

This is what I get from the logs

connect:dispatcher swaggerMetadata  : /v1/authorise/_latesta +1m
  swagger-tools:middleware:metadata GET /v1/authorise/_latesta +1m
  swagger-tools:middleware:metadata   Is a Swagger path: false +0ms
  connect:dispatcher swaggerSecurity  : /v1/authorise/_latesta +1ms
  swagger-tools:middleware:security GET /v1/authorise/_latesta +1m
  swagger-tools:middleware:security   Will process: no +0ms
  connect:dispatcher swaggerValidator  : /v1/authorise/_latesta +1ms
  swagger-tools:middleware:validator GET /v1/authorise/_latesta +1m
  swagger-tools:middleware:validator   Will process: no +0ms
  connect:dispatcher swaggerRouter  : /v1/authorise/_latesta +0ms
  swagger-tools:middleware:router GET /v1/authorise/_latesta +1m
  swagger-tools:middleware:router   Will process: no +0ms
  connect:dispatcher swaggerUI  : /v1/authorise/_latesta +1ms
  swagger-tools:middleware:ui GET /v1/authorise/_latesta +1m
  swagger-tools:middleware:ui   Will process: no +0ms
  connect:dispatcher helmet  : /v1/authorise/_latesta +0ms
  connect:dispatcher logger  : /v1/authorise/_latesta +2ms
  connect:dispatcher error  : /v1/authorise/_latesta +1ms
  finalhandler default 404 +2ms

Is there a way I can use the Is a Swagger path: false message? If I can use it, then I can handle the error as I like.

Thank you.