This is a middy middleware, which is formatting errors as error responses according to the JSON:API spec.
npm install @joblocal/middy-json-api-error-handler
# handler.js
const middy = require('middy');
const jsonApiErrorHandler = require('@joblocal/middy-json-api-error-handler');
const yourHandler = () => {
throw new Error({
code: '123',
source: { 'pointer': '/data/attributes/first-name' },
title: 'Value is too short',
detail: 'First name must contain at least three characters.'
});
};
const handler = middy(yourHandler)
.use(jsonApiErrorHandler());
module.exports = { handler };