SW-3572 API route for listing hotels per city or country * wip hotel data endpoint * Correct route params type * wip * skip static paths call * timeout when getting destinations take too long * call noStore when we get a timeout * add cache-control headers * . * . * . * wip * wip * wip * wip * add route for getting hotels per country * include city when listing by country * fix distance SI unit * fix sorting * Merge branch 'master' of bitbucket.org:scandic-swap/web into feature/SW-3572-hotel-data-endpoint * packages/tracking passWithNoTests * revalidate must be static value * remove oxc reference * cleanup * cleanup hotel api route * feat(SW-3572): cleanup error handling Approved-by: Anton Gunnarsson
126 lines
2.6 KiB
TypeScript
126 lines
2.6 KiB
TypeScript
import { TRPCError } from "@trpc/server"
|
|
|
|
export function gatewayTimeout(cause?: unknown) {
|
|
return new TRPCError({
|
|
code: "GATEWAY_TIMEOUT",
|
|
message: `Gateway Timeout`,
|
|
cause,
|
|
})
|
|
}
|
|
|
|
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 httpStatusByErrorCode(error: TRPCError) {
|
|
switch (error.code) {
|
|
case "BAD_REQUEST":
|
|
return 400
|
|
case "UNAUTHORIZED":
|
|
return 401
|
|
case "FORBIDDEN":
|
|
return 403
|
|
case "NOT_FOUND":
|
|
return 404
|
|
case "CONFLICT":
|
|
return 409
|
|
case "UNPROCESSABLE_CONTENT":
|
|
return 422
|
|
case "GATEWAY_TIMEOUT":
|
|
return 504
|
|
case "INTERNAL_SERVER_ERROR":
|
|
default:
|
|
return 500
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|