mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-06 00:07:44 +01:00
51d73f67a3
This PR aims to handle the increased log alarm volume seen by the SREs. It appears that we get a large number of alarms because a client disconnects early from the front-end API. These errors are then converted into 500s because of missing handling. These errors appear to be caused by the `http-errors` library in a dependency. We also introduced a log line whenever we see errors now a while back, and I don't think we need this logging (I was also the one who introduced it). The changes in this PR are specifically: - When converting from arbitrary errors, give `BadRequestError` a 400 status code, not a 500. - Add a dependency on `http-errors` (which is already a transitive dependency because of the body parser) and use that to check whether an error is an http-error. - If an error is an http error, then propagate it to the user with the status code and message. - Remove warning logs when an error occurs. This was introduced to make it easier to correlate an API error and the logs, but the system hasn't been set up for that (yet?), so it's just noise now. - When logging errors as errors, only do that if their status code would be 500.
64 lines
2.0 KiB
TypeScript
64 lines
2.0 KiB
TypeScript
import joi from 'joi';
|
|
import { Response } from 'express';
|
|
import { Logger } from '../logger';
|
|
import { UnleashError } from '../error/unleash-error';
|
|
import { fromLegacyError } from '../error/from-legacy-error';
|
|
import createError from 'http-errors';
|
|
|
|
export const customJoi = joi.extend((j) => ({
|
|
type: 'isUrlFriendly',
|
|
base: j.string(),
|
|
messages: {
|
|
'isUrlFriendly.base': '{{#label}} must be URL friendly',
|
|
},
|
|
validate(value, helpers) {
|
|
// Base validation regardless of the rules applied
|
|
if (encodeURIComponent(value) !== value) {
|
|
// Generate an error, state and options need to be passed
|
|
return { value, errors: helpers.error('isUrlFriendly.base') };
|
|
}
|
|
return undefined;
|
|
},
|
|
}));
|
|
|
|
export const nameType = customJoi.isUrlFriendly().min(1).max(100).required();
|
|
|
|
export const handleErrors: (
|
|
res: Response,
|
|
logger: Logger,
|
|
error: Error,
|
|
) => void = (res, logger, error) => {
|
|
if (createError.isHttpError(error)) {
|
|
return (
|
|
res
|
|
// @ts-expect-error http errors all have statuses, but there are no
|
|
// types provided
|
|
.status(error.status ?? 400)
|
|
.json({ message: error.message })
|
|
.end()
|
|
);
|
|
}
|
|
|
|
const finalError =
|
|
error instanceof UnleashError ? error : fromLegacyError(error);
|
|
|
|
const format = (thing: object) => JSON.stringify(thing, null, 2);
|
|
|
|
if (!(error instanceof UnleashError)) {
|
|
logger.debug(
|
|
`I encountered an error that wasn't an instance of the \`UnleashError\` type. The original error was: ${format(
|
|
error,
|
|
)}. It was mapped to ${format(finalError.toJSON())}`,
|
|
);
|
|
}
|
|
|
|
if (finalError.statusCode === 500) {
|
|
logger.error(
|
|
`Server failed executing request: ${format(error)}`,
|
|
error,
|
|
);
|
|
}
|
|
|
|
return res.status(finalError.statusCode).json(finalError).end();
|
|
};
|