Using an operation handler for requests, I have defined the creation of a user in the oas scheme as follows:
/users:
post:
description: |
creates a user
operationId: createUser
x-eov-operation-handler: controllers/user.controller
tags:
- Users
requestBody:
description: User to create
required: true
content:
application/json:
schema:
properties:
email:
type: string
description: The email of the user
example: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85e0fde4e8f5e9e0c5e2e8e4ece9abe6eae8">[email protected]</a>
pattern: ^[\w-\.]+@([\w-]+\.)+[\w-]{2,5}$
password:
type: string
description: The password of the user
example: password1234
minLength: 8
maxLength: 16
required:
- email
- password
additionalProperties: false
responses:
'201':
description: User was created
content:
application/json:
schema:
$ref: '#/components/schemas/User'
'400':
description: Bad request
content:
application/json:
schema:
properties:
code:
description: Http status code
type: number
example: 400
message:
description: The message of the response
type: string
example: Bad request
required:
- code
- message
additionalProperties: false
... etc
Here are my validator settings:
const openApiValidator = OpenApiValidator.middleware({
apiSpec,
operationHandlers: path.join(__dirname)
})
However, when attempting to create a user with an incorrect email pattern like examplegmail.com, instead of receiving something like: {400,'Bad request'}
,
I am presented with an HTML page.
<!DOCTYPE html>
<html lang="en>
<head>
<meta charset="utf-8">
<title>Error</title></head>
<body>
<pre>Error details here...
</pre>
</body>
</html>
Is there a way to change this behavior or is it intended to be this way?
Edit: I also have an error handler defined after openApiValidator
app.use(openApiValidator);
app.use(errorHandler);
function errorHandler(err: any, req: Request, res: Response) {
res.status(err.status || 500).json({
code: err.status,
message: err.message,
});
}
Edit 2: Issue resolved After realizing that my error handler was missing the next parameter, I included it and the problem was fixed.