mirror of
https://github.com/outline/outline.git
synced 2026-12-06 11:12:32 +03:00
* fix: Keep HTTP errors with "not found" in their message from becoming 404 * test: Expect 400 for an anchorNodeId that is not in the document
119 lines
3.4 KiB
TypeScript
119 lines
3.4 KiB
TypeScript
import httpErrors from "http-errors";
|
|
import type { Next } from "koa";
|
|
import {
|
|
ValidationError as SequelizeValidationError,
|
|
EmptyResultError as SequelizeEmptyResultError,
|
|
DatabaseError as SequelizeDatabaseError,
|
|
} from "sequelize";
|
|
import {
|
|
AuthorizationError,
|
|
DatabaseReadOnlyError,
|
|
NotFoundError,
|
|
RequestTimeoutError,
|
|
ServiceUnavailableError,
|
|
ValidationError,
|
|
} from "@server/errors";
|
|
import Metrics from "@server/logging/Metrics";
|
|
import {
|
|
isDatabaseReadOnlyError,
|
|
isDatabaseUnavailableError,
|
|
isQueryCanceledError,
|
|
} from "@server/storage/database";
|
|
import type { AppContext } from "@server/types";
|
|
|
|
/** Postgres codes for a requested date the database cannot represent. */
|
|
const DATE_RANGE_ERROR_CODES = new Set(["22008", "22015"]);
|
|
|
|
/**
|
|
* Reads the SQLSTATE code from the driver error a DatabaseError wraps.
|
|
*
|
|
* @param err the Sequelize error to inspect.
|
|
* @returns the SQLSTATE code, or undefined if the driver did not supply one.
|
|
*/
|
|
function sqlStateOf(err: SequelizeDatabaseError): string | undefined {
|
|
const { parent } = err;
|
|
if (parent && "code" in parent && typeof parent.code === "string") {
|
|
return parent.code;
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
export default function apiErrorHandler() {
|
|
return async function apiErrorHandlerMiddleware(ctx: AppContext, next: Next) {
|
|
try {
|
|
await next();
|
|
} catch (err) {
|
|
let transformedErr = err;
|
|
|
|
if (
|
|
!(err instanceof AuthorizationError) &&
|
|
err instanceof Error &&
|
|
/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 instanceof SequelizeDatabaseError &&
|
|
DATE_RANGE_ERROR_CODES.has(sqlStateOf(err) ?? "")
|
|
) {
|
|
transformedErr = ValidationError(
|
|
"Date value in request is out of range"
|
|
);
|
|
}
|
|
|
|
if (isQueryCanceledError(err)) {
|
|
transformedErr = RequestTimeoutError();
|
|
}
|
|
|
|
if (isDatabaseReadOnlyError(err)) {
|
|
Metrics.increment("database.read_only", { source: "api" });
|
|
transformedErr = DatabaseReadOnlyError();
|
|
}
|
|
|
|
if (isDatabaseUnavailableError(err)) {
|
|
Metrics.increment("database.unavailable", { source: "api" });
|
|
transformedErr = ServiceUnavailableError();
|
|
}
|
|
|
|
if (
|
|
(err instanceof Error && "code" in err && err.code === "ENOENT") ||
|
|
err instanceof SequelizeEmptyResultError ||
|
|
(err instanceof Error &&
|
|
!httpErrors.isHttpError(err) &&
|
|
/Not found/i.test(err.message))
|
|
) {
|
|
transformedErr = NotFoundError();
|
|
}
|
|
|
|
// RFC 6750 §3: a 401 must carry a challenge for the Bearer scheme.
|
|
if (
|
|
httpErrors.isHttpError(transformedErr) &&
|
|
transformedErr.status === 401
|
|
) {
|
|
const headers = transformedErr.headers ?? {};
|
|
const hasChallenge =
|
|
!!ctx.response.get("WWW-Authenticate") ||
|
|
Object.keys(headers).some(
|
|
(key) => key.toLowerCase() === "www-authenticate"
|
|
);
|
|
if (!hasChallenge) {
|
|
transformedErr.headers = { ...headers, "WWW-Authenticate": "Bearer" };
|
|
}
|
|
}
|
|
|
|
throw transformedErr;
|
|
}
|
|
};
|
|
}
|