Files
web/packages/trpc/lib/errors.ts
Anton Gunnarsson 846fd904a6 Merged in feat/sw-2859-set-up-shared-trpc-package (pull request #2319)
feat(SW-2859): Create trpc package

* Add isEdge, safeTry and dataCache to new common package

* Add eslint and move prettier config

* Clean up tests

* Create trpc package and move initialization

* Move errors and a few procedures

* Move telemetry to common package

* Move tokenManager to common package

* Add Sentry to procedures

* Clean up procedures

* Fix self-referencing imports

* Add exports to packages and lint rule to prevent relative imports

* Add env to trpc package

* Add eslint to trpc package

* Apply lint rules

* Use direct imports from trpc package

* Add lint-staged config to trpc

* Move lang enum to common

* Restructure trpc package folder structure

* Fix lang imports


Approved-by: Linus Flood
2025-06-18 12:14:20 +00:00

96 lines
2.1 KiB
TypeScript

import { TRPCError } from "@trpc/server"
export function unauthorizedError(cause?: unknown) {
return new TRPCError({
code: "UNAUTHORIZED",
message: `Unauthorized`,
cause,
})
}
export function forbiddenError(cause?: unknown) {
return new TRPCError({
code: "FORBIDDEN",
message: `Forbidden`,
cause,
})
}
export function conflictError(cause?: unknown) {
return new TRPCError({
code: "CONFLICT",
message: `Conflict`,
cause,
})
}
export function badRequestError(cause?: unknown) {
return new TRPCError({
code: "BAD_REQUEST",
message: `Bad request`,
cause,
})
}
export function notFound(cause?: unknown) {
return new TRPCError({
code: "NOT_FOUND",
message: `Not found`,
cause,
})
}
export function unprocessableContent(cause?: unknown) {
return new TRPCError({
code: "UNPROCESSABLE_CONTENT",
message: "Unprocessable content",
cause,
})
}
export function internalServerError(cause?: unknown) {
return new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `Internal Server Error`,
cause,
})
}
export const SESSION_EXPIRED = "SESSION_EXPIRED"
export class SessionExpiredError extends Error {}
export function sessionExpiredError() {
return new TRPCError({
code: "UNAUTHORIZED",
message: SESSION_EXPIRED,
cause: new SessionExpiredError(SESSION_EXPIRED),
})
}
export const PUBLIC_UNAUTHORIZED = "PUBLIC_UNAUTHORIZED"
export class PublicUnauthorizedError extends Error {}
export function publicUnauthorizedError() {
return new TRPCError({
code: "UNAUTHORIZED",
message: PUBLIC_UNAUTHORIZED,
cause: new PublicUnauthorizedError(PUBLIC_UNAUTHORIZED),
})
}
export function serverErrorByStatus(status: number, cause?: unknown) {
switch (status) {
case 401:
return unauthorizedError(cause)
case 403:
return forbiddenError(cause)
case 404:
return notFound(cause)
case 409:
return conflictError(cause)
case 422:
return unprocessableContent(cause)
case 500:
default:
return internalServerError(cause)
}
}