mirror of
https://github.com/outline/outline.git
synced 2026-12-05 02:44:34 +03:00
* chore: Rename APIContext to Context and flatten its fields Auth and transaction now live directly on the context instead of in ctx.state and a nested ctx.context, and model helpers read them directly. APIContext remains as a deprecated alias. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf * chore: Remove deprecated APIContext alias Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf * fix: Read auth from context in OAuth provider callbacks Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf * chore: Add withUser and withTransaction context helpers Replaces manual createContext rebuilds that copy fields from an existing context. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf * chore: Remove arbitrary property access from Koa context types AppContext and Context no longer inherit the any index signature from Koa's DefaultContext, custom fields are declared explicitly, and importing Koa's loose Context type is disallowed by lint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf --------- Co-authored-by: Claude <noreply@anthropic.com>
117 lines
3.4 KiB
TypeScript
117 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 && /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;
|
|
}
|
|
};
|
|
}
|