Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
93 lines
3.4 KiB
Plaintext
93 lines
3.4 KiB
Plaintext
import { status as httpStatus } from 'http-status';
|
|
import { APIError } from '../errors/APIError.js';
|
|
import { getPayload } from '../index.js';
|
|
import { formatErrors } from './formatErrors.js';
|
|
import { headersWithCors } from './headersWithCors.js';
|
|
import { isErrorPublic } from './isErrorPublic.js';
|
|
import { logError } from './logError.js';
|
|
import { mergeHeaders } from './mergeHeaders.js';
|
|
export const routeError = async ({ collection, config: configArg, err, req: incomingReq })=>{
|
|
if ('payloadInitError' in err && err.payloadInitError === true) {
|
|
// do not attempt initializing Payload if the error is due to a failed initialization. Otherwise,
|
|
// it will cause an infinite loop of initialization attempts and endless error responses, without
|
|
// actually logging the error, as the error logging code will never be reached.
|
|
console.error(err);
|
|
return Response.json({
|
|
message: 'There was an error initializing Payload'
|
|
}, {
|
|
status: httpStatus.INTERNAL_SERVER_ERROR
|
|
});
|
|
}
|
|
let payload = incomingReq && 'payload' in incomingReq && incomingReq?.payload;
|
|
if (!payload) {
|
|
try {
|
|
payload = await getPayload({
|
|
config: configArg,
|
|
cron: true
|
|
});
|
|
} catch (ignore) {
|
|
return Response.json({
|
|
message: 'There was an error initializing Payload'
|
|
}, {
|
|
status: httpStatus.INTERNAL_SERVER_ERROR
|
|
});
|
|
}
|
|
}
|
|
let response = formatErrors(err);
|
|
let status = err.status || httpStatus.INTERNAL_SERVER_ERROR;
|
|
logError({
|
|
err,
|
|
payload
|
|
});
|
|
const req = incomingReq;
|
|
req.payload = payload;
|
|
const headers = headersWithCors({
|
|
headers: new Headers(),
|
|
req
|
|
});
|
|
const { config } = payload;
|
|
// Internal server errors can contain anything, including potentially sensitive data.
|
|
// Therefore, error details will be hidden from the response unless `config.debug` is `true`
|
|
if (!isErrorPublic(err, config)) {
|
|
response = formatErrors(new APIError('Something went wrong.'));
|
|
}
|
|
if (config.debug && config.debug === true) {
|
|
response.stack = err.stack;
|
|
}
|
|
if (collection) {
|
|
await collection.config.hooks.afterError?.reduce(async (promise, hook)=>{
|
|
await promise;
|
|
const result = await hook({
|
|
collection: collection.config,
|
|
context: req.context,
|
|
error: err,
|
|
req,
|
|
result: response
|
|
});
|
|
if (result) {
|
|
response = result.response || response;
|
|
status = result.status || status;
|
|
}
|
|
}, Promise.resolve());
|
|
}
|
|
await config.hooks.afterError?.reduce(async (promise, hook)=>{
|
|
await promise;
|
|
const result = await hook({
|
|
collection: collection?.config,
|
|
context: req.context,
|
|
error: err,
|
|
req,
|
|
result: response
|
|
});
|
|
if (result) {
|
|
response = result.response || response;
|
|
status = result.status || status;
|
|
}
|
|
}, Promise.resolve());
|
|
return Response.json(response, {
|
|
headers: req.responseHeaders ? mergeHeaders(req.responseHeaders, headers) : headers,
|
|
status
|
|
});
|
|
};
|
|
|
|
//# sourceMappingURL=routeError.js.map |