Skip to content

Commit 05d17d9

Browse files
committed
export APIError
1 parent e6e48bb commit 05d17d9

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

lib/client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CreateNewRequest, CreateNewResponse, GetTestMail, Problem, WaitForMessageResponse } from "./models";
1+
import { APIError, CreateNewRequest, CreateNewResponse, GetTestMail, Problem, WaitForMessageResponse } from "./models";
22

33
class GetTestMailClient {
44
private apiKey: string;
@@ -21,7 +21,7 @@ class GetTestMailClient {
2121

2222
if (!response.ok) {
2323
const problem: Problem = await response.json();
24-
throw new Error(problem.detail);
24+
throw new APIError(problem);
2525
}
2626

2727
const getTestMail: GetTestMail = await response.json();
@@ -40,7 +40,7 @@ class GetTestMailClient {
4040

4141
if (!response.ok) {
4242
const problem: Problem = await response.json();
43-
throw new Error(problem.detail);
43+
throw new APIError(problem);
4444
}
4545

4646
const getTestMail: GetTestMail = await response.json();

lib/models.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ export interface Problem {
2727
status: number;
2828
}
2929

30+
export class APIError extends Error {
31+
type: string;
32+
title: string;
33+
detail: string;
34+
status: number;
35+
constructor(problem: Problem) {
36+
super(problem.detail);
37+
this.type = problem.type;
38+
this.title = problem.title;
39+
this.detail = problem.detail;
40+
this.status = problem.status;
41+
}
42+
}
43+
3044
export type CreateNewRequest = {
3145
expiresAt?: string;
3246
};

0 commit comments

Comments
 (0)