summaryrefslogtreecommitdiff
path: root/u/server/filter/mod.ts
blob: 3b247fce47d0e4fa941f2688f9d9a061e789ae37 (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
35
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(
    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): Promise<IEither<Err, T>>;
}

export * from "./method.ts";
export * from "./json.ts";