All files / packages/content/src/core/domain errors.ts

100% Statements 16/16
100% Branches 8/8
100% Functions 9/9
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81    100x 100x     100x 100x           12x       31x     31x   31x                   3x           16x                   18x   18x           6x                 16x           8x           2x      
export class ContentError extends Error {
    constructor(
        public readonly statusCode: number,
        public readonly statusMessage: string,
        options?: ErrorOptions,
    ) {
        super(statusMessage, options);
        this.name = new.target.name;
    }
}
 
// 400 and 404 are the caller's fault. Everything else (wrong credentials, etc.)
// are gateways failures, not client errors.
const PASSTHROUGH_STATUSES = new Set([400, 404]);
 
export class UpstreamError extends ContentError {
    constructor(
        public readonly upstreamStatus: number | undefined,
        cause?: unknown,
        // Not `data`, which h3's createError would hand to the client
        public readonly body?: unknown,
    ) {
        super(
            upstreamStatus && PASSTHROUGH_STATUSES.has(upstreamStatus) ? upstreamStatus : 502,
            "Upstream request failed",
            { cause },
        );
    }
}
 
export class ContentUnavailableError extends ContentError {
    constructor(resource: string, cause?: unknown) {
        super(502, `Content unavailable for "${resource}"`, { cause });
    }
}
 
export class UndefinedVendorError extends ContentError {
    constructor(name: string | undefined, available: readonly string[]) {
        super(
            500,
            `Requested vendor "${name ?? null}" does not exist. Available: ${available.join(", ")}`,
        );
    }
}
 
export class MisconfiguredVendorError extends ContentError {
    constructor(
        vendor: string,
        public readonly problems: string[],
    ) {
        super(500, `${vendor} is misconfigured: ${problems.join(", ")}`);
    }
}
 
export class UndefinedResourceError extends ContentError {
    constructor(resource: string | undefined, available: readonly string[]) {
        super(
            404,
            `Requested resource "${resource ?? null}" does not exist. Available: ${available.join(", ")}`,
        );
    }
}
 
export class MalformedQueryError extends ContentError {
    constructor(param: string, expected: string) {
        super(400, `Query parameter "${param}" is malformed, expected ${expected}`);
    }
}
 
export class NotFoundError extends ContentError {
    constructor(resource: string, slug: string) {
        super(404, `No "${resource}" found with slug "${slug}"`);
    }
}
 
export class PageNotFoundError extends ContentError {
    constructor(resource: string, page: number) {
        super(404, `"${resource}" has no page ${page}`);
    }
}