Skip to content

Commit c4c8c78

Browse files
authored
feat(piefed): PiefedResponseError, improve error handling (#30)
1 parent 29f118c commit c4c8c78

5 files changed

Lines changed: 45 additions & 4 deletions

File tree

src/errors.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { PiefedErrorResponse } from "./types";
2+
13
export class FediverseError extends Error {
24
constructor(message: string, errorOptions?: ErrorOptions) {
35
super(message, errorOptions);
@@ -12,6 +14,25 @@ export class InvalidPayloadError extends FediverseError {
1214
}
1315
}
1416

17+
export class ResponseError extends FediverseError {
18+
status: number;
19+
20+
constructor(status: number, message?: string) {
21+
super(message ?? `${status}`);
22+
this.status = status;
23+
this.name = "ResponseError";
24+
}
25+
}
26+
export class PiefedResponseError extends ResponseError {
27+
response: PiefedErrorResponse;
28+
29+
constructor(status: number, payload: PiefedErrorResponse) {
30+
super(status, payload.message);
31+
this.response = payload;
32+
this.name = "PiefedResponseError";
33+
}
34+
}
35+
1536
export class UnexpectedResponseError extends FediverseError {
1637
constructor(message: string) {
1738
super(message);

src/providers/piefed/index.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ import {
55
BaseClientOptions,
66
RequestOptions,
77
} from "../../BaseClient";
8-
import { InvalidPayloadError, UnsupportedError } from "../../errors";
8+
import {
9+
InvalidPayloadError,
10+
PiefedResponseError,
11+
ResponseError,
12+
UnsupportedError,
13+
} from "../../errors";
914
import { cleanThreadiverseParams } from "../../helpers";
1015
import buildSafeClient from "../../SafeClient";
16+
import { PiefedErrorResponse } from "../../schemas";
1117
import {
1218
ListPersonContent,
1319
ListPersonContentResponse,
@@ -23,9 +29,13 @@ import { components, paths } from "./schema";
2329
async function validateResponse(response: Response) {
2430
if (!response.ok) {
2531
const data = await response.json();
26-
if ("error" in data && typeof data.error === "string") {
27-
throw new Error(data.error);
28-
}
32+
33+
const parsed = PiefedErrorResponse.safeParse(data);
34+
35+
if (parsed.success)
36+
throw new PiefedResponseError(response.status, parsed.data);
37+
38+
throw new ResponseError(response.status);
2939
}
3040
}
3141

src/schemas/PiefedErrorResponse.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { z } from "zod/v4-mini";
2+
3+
export const PiefedErrorResponse = z.object({
4+
code: z.number(),
5+
message: z.string(),
6+
status: z.string(),
7+
});

src/schemas/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,7 @@ export * from "./SiteAggregates";
4949
export * from "./SubscribedType";
5050
export * from "./UploadImageResponse";
5151

52+
export * from "./PiefedErrorResponse";
53+
5254
// Should be last (can't access lexical declaration 'PersonView' before initialization)
5355
export * from "./list";

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export type PersonContentItem = z.infer<typeof schemas.PersonContentItem>;
122122
export type PersonMention = z.infer<typeof schemas.PersonMention>;
123123
export type PersonMentionView = z.infer<typeof schemas.PersonMentionView>;
124124
export type PersonView = z.infer<typeof schemas.PersonView>;
125+
export type PiefedErrorResponse = z.infer<typeof schemas.PiefedErrorResponse>;
125126
export type Post = z.infer<typeof schemas.Post>;
126127
export type PostAggregates = z.infer<typeof schemas.PostAggregates>;
127128
export type PostReport = z.infer<typeof schemas.PostReport>;

0 commit comments

Comments
 (0)