mirror of
https://github.com/outline/outline.git
synced 2026-01-06 02:59:54 -06:00
chore: Add API error handler middleware (#8572)
This commit is contained in:
@@ -6,18 +6,8 @@ import Koa from "koa";
|
||||
import escape from "lodash/escape";
|
||||
import isNil from "lodash/isNil";
|
||||
import snakeCase from "lodash/snakeCase";
|
||||
import {
|
||||
ValidationError as SequelizeValidationError,
|
||||
EmptyResultError as SequelizeEmptyResultError,
|
||||
} from "sequelize";
|
||||
import env from "@server/env";
|
||||
import {
|
||||
AuthorizationError,
|
||||
ClientClosedRequestError,
|
||||
InternalError,
|
||||
NotFoundError,
|
||||
ValidationError,
|
||||
} from "@server/errors";
|
||||
import { ClientClosedRequestError, InternalError } from "@server/errors";
|
||||
import { requestErrorHandler } from "@server/logging/sentry";
|
||||
|
||||
let errorHtmlCache: Buffer | undefined;
|
||||
@@ -32,16 +22,6 @@ export default function onerror(app: Koa) {
|
||||
|
||||
err = wrapInNativeError(err);
|
||||
|
||||
if (err instanceof SequelizeValidationError) {
|
||||
if (err.errors && err.errors[0]) {
|
||||
err = ValidationError(
|
||||
`${err.errors[0].message} (${err.errors[0].path})`
|
||||
);
|
||||
} else {
|
||||
err = ValidationError();
|
||||
}
|
||||
}
|
||||
|
||||
// Client aborted errors are a 500 by default, but 499 is more appropriate
|
||||
if (err instanceof formidable.errors.FormidableError) {
|
||||
if (err.internalCode === 1002) {
|
||||
@@ -49,21 +29,6 @@ export default function onerror(app: Koa) {
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
err.code === "ENOENT" ||
|
||||
err instanceof SequelizeEmptyResultError ||
|
||||
/Not found/i.test(err.message)
|
||||
) {
|
||||
err = NotFoundError();
|
||||
}
|
||||
|
||||
if (
|
||||
!(err instanceof AuthorizationError) &&
|
||||
/Authorization error/i.test(err.message)
|
||||
) {
|
||||
err = AuthorizationError();
|
||||
}
|
||||
|
||||
// Push only unknown and 500 status errors to sentry
|
||||
if (
|
||||
typeof err.status !== "number" ||
|
||||
|
||||
@@ -22,6 +22,7 @@ import groupMemberships from "./groupMemberships";
|
||||
import groups from "./groups";
|
||||
import installation from "./installation";
|
||||
import integrations from "./integrations";
|
||||
import apiErrorHandler from "./middlewares/apiErrorHandler";
|
||||
import apiResponse from "./middlewares/apiResponse";
|
||||
import apiTracer from "./middlewares/apiTracer";
|
||||
import editor from "./middlewares/editor";
|
||||
@@ -60,6 +61,7 @@ api.use(coalesceBody());
|
||||
api.use<BaseContext, UserAgentContext>(userAgent);
|
||||
api.use(apiTracer());
|
||||
api.use(apiResponse());
|
||||
api.use(apiErrorHandler());
|
||||
api.use(editor());
|
||||
|
||||
// Register plugin API routes before others to allow for overrides
|
||||
|
||||
47
server/routes/api/middlewares/apiErrorHandler.ts
Normal file
47
server/routes/api/middlewares/apiErrorHandler.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { Context, Next } from "koa";
|
||||
import {
|
||||
ValidationError as SequelizeValidationError,
|
||||
EmptyResultError as SequelizeEmptyResultError,
|
||||
} from "sequelize";
|
||||
import {
|
||||
AuthorizationError,
|
||||
NotFoundError,
|
||||
ValidationError,
|
||||
} from "@server/errors";
|
||||
|
||||
export default function apiErrorHandler() {
|
||||
return async function apiErrorHandlerMiddleware(ctx: Context, next: Next) {
|
||||
try {
|
||||
await next();
|
||||
} catch (err) {
|
||||
let transformedErr = err;
|
||||
|
||||
if (
|
||||
!(err instanceof AuthorizationError) &&
|
||||
/Authorization error/i.test(err.message)
|
||||
) {
|
||||
transformedErr = AuthorizationError();
|
||||
}
|
||||
|
||||
if (err instanceof SequelizeValidationError) {
|
||||
if (err.errors && err.errors[0]) {
|
||||
transformedErr = ValidationError(
|
||||
`${err.errors[0].message} (${err.errors[0].path})`
|
||||
);
|
||||
} else {
|
||||
transformedErr = ValidationError();
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
err.code === "ENOENT" ||
|
||||
err instanceof SequelizeEmptyResultError ||
|
||||
/Not found/i.test(err.message)
|
||||
) {
|
||||
transformedErr = NotFoundError();
|
||||
}
|
||||
|
||||
throw transformedErr;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user