Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
express-openapi: allow to registered errorMiddleware when exposeApiDo…
- Loading branch information
1 parent
506a3a4
commit 0120f00
Showing
5 changed files
with
112 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...enapi/test/sample-projects/with-errorMiddleware-and-exposeApiDocs-set-to-false/api-doc.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// args.apiDoc needs to be a js object. This file could be a json file, but we can't add | ||
// comments in json files. | ||
module.exports = { | ||
swagger: '2.0', | ||
|
||
// all routes will now have /v3 prefixed. | ||
basePath: '/v3', | ||
|
||
info: { | ||
title: 'express-openapi sample project', | ||
version: '3.0.0' | ||
}, | ||
|
||
definitions: { | ||
Foo: { | ||
type: 'string' | ||
} | ||
}, | ||
|
||
// paths are derived from args.routes. These are filled in by fs-routes. | ||
paths: {} | ||
}; |
16 changes: 16 additions & 0 deletions
16
...est/sample-projects/with-errorMiddleware-and-exposeApiDocs-set-to-false/api-routes/foo.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module.exports = { | ||
get: function(req, res, next) { | ||
next(new Error('hello from /v3/foo')); | ||
} | ||
}; | ||
|
||
module.exports.get.apiDoc = { | ||
description: 'Get foo.', | ||
operationId: 'getFoo', | ||
parameters: [], | ||
responses: { | ||
200: { | ||
description: 'testing error handler' | ||
} | ||
} | ||
}; |
34 changes: 34 additions & 0 deletions
34
...s-openapi/test/sample-projects/with-errorMiddleware-and-exposeApiDocs-set-to-false/app.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
var app = require('express')(); | ||
var bodyParser = require('body-parser'); | ||
// normally you'd just do require('express-openapi'), but this is for test purposes. | ||
var openapi = require('../../../'); | ||
var path = require('path'); | ||
var cors = require('cors'); | ||
|
||
app.use(cors()); | ||
app.use(bodyParser.json()); | ||
|
||
app.get('/foo', function(req, res, next) { | ||
next(new Error('hello from /foo')); | ||
}); | ||
|
||
openapi.initialize({ | ||
apiDoc: require('./api-doc.js'), | ||
app: app, | ||
paths: path.resolve(__dirname, 'api-routes'), | ||
exposeApiDocs: false, | ||
errorMiddleware: function(err, req, res, next) { | ||
res.status(200).json(err.message); | ||
} | ||
}); | ||
|
||
app.use(function(err, req, res, next) { | ||
res.status(200).json(err.message); | ||
}); | ||
|
||
module.exports = app; | ||
|
||
var port = parseInt(process.argv[2], 10); | ||
if (port) { | ||
app.listen(port); | ||
} |
23 changes: 23 additions & 0 deletions
23
...-openapi/test/sample-projects/with-errorMiddleware-and-exposeApiDocs-set-to-false/spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
var app; | ||
var expect = require('chai').expect; | ||
var request = require('supertest'); | ||
|
||
before(function() { | ||
app = require('./app.js'); | ||
}); | ||
|
||
describe('when an error occurs in the basePath', function() { | ||
it('should use the API error middleware', function(done) { | ||
request(app) | ||
.get('/v3/foo') | ||
.expect(200, '"hello from /v3/foo"', done); | ||
}); | ||
}); | ||
|
||
describe('when an error occurs outside the basePath', function() { | ||
it('should not use the API error middleware', function(done) { | ||
request(app) | ||
.get('/foo') | ||
.expect(200, '"hello from /foo"', done); | ||
}); | ||
}); |