summaryrefslogtreecommitdiff
path: root/u/server/filter/index.ts
blob: 75168c787790e67a051a25f0cf03b898c36a5273 (plain)
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
import {
    type IEither,
    type ITraceable,
    LogLevel,
    type PenguenoRequest,
    type ServerTrace,
} from '@emprespresso/pengueno';

export enum ErrorSource {
    USER = LogLevel.WARN,
    SYSTEM = LogLevel.ERROR,
}

export class PenguenoError extends Error {
    public readonly source: ErrorSource;
    constructor(
        override readonly message: string,
        public readonly status: number,
    ) {
        super(message);
        this.source = Math.floor(status / 100) === 4 ? ErrorSource.USER : ErrorSource.SYSTEM;
    }
}

export interface RequestFilter<
    T,
    Err extends PenguenoError = PenguenoError,
    RIn = ITraceable<PenguenoRequest, ServerTrace>,
> {
    (req: RIn): IEither<Err, T> | Promise<IEither<Err, T>>;
}

export * from './method.js';
export * from './json.js';