feat: map and expose objection.js errors
This commit is contained in:
@@ -1,8 +1,9 @@
|
|||||||
import logger from './logger.js';
|
import logger from './logger.js';
|
||||||
import objection from 'objection';
|
import objection from 'objection';
|
||||||
import * as Sentry from './sentry.ee.js';
|
import * as Sentry from './sentry.ee.js';
|
||||||
const { NotFoundError, DataError } = objection;
|
const { NotFoundError, DataError, ValidationError } = objection;
|
||||||
import HttpError from '../errors/http.js';
|
import HttpError from '../errors/http.js';
|
||||||
|
import { renderObjectionError } from './renderer.js';
|
||||||
|
|
||||||
// Do not remove `next` argument as the function signature will not fit for an error handler middleware
|
// Do not remove `next` argument as the function signature will not fit for an error handler middleware
|
||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
@@ -15,6 +16,10 @@ const errorHandler = (error, request, response, next) => {
|
|||||||
response.status(404).end();
|
response.status(404).end();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (error instanceof ValidationError) {
|
||||||
|
renderObjectionError(response, error);
|
||||||
|
}
|
||||||
|
|
||||||
if (error instanceof DataError) {
|
if (error instanceof DataError) {
|
||||||
response.status(400).end();
|
response.status(400).end();
|
||||||
}
|
}
|
||||||
|
@@ -64,4 +64,22 @@ const renderError = (response, errors, status, type) => {
|
|||||||
return response.status(errorStatus).send(payload);
|
return response.status(errorStatus).send(payload);
|
||||||
};
|
};
|
||||||
|
|
||||||
export { renderObject, renderError };
|
const renderObjectionError = (response, error) => {
|
||||||
|
const {
|
||||||
|
statusCode,
|
||||||
|
type,
|
||||||
|
data = {},
|
||||||
|
} = error;
|
||||||
|
|
||||||
|
const errorEntries = Object.entries(data);
|
||||||
|
|
||||||
|
const computedErrors = errorEntries.reduce((errors, [fieldName, fieldErrors]) => {
|
||||||
|
const computedErrors = fieldErrors.map(fieldError => fieldError.message);
|
||||||
|
|
||||||
|
return errors.concat({ [fieldName]: computedErrors });
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return renderError(response, computedErrors, statusCode, type);
|
||||||
|
};
|
||||||
|
|
||||||
|
export { renderObject, renderError, renderObjectionError };
|
||||||
|
Reference in New Issue
Block a user