File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import { PiefedErrorResponse } from "./types" ;
2+
13export 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+
1536export class UnexpectedResponseError extends FediverseError {
1637 constructor ( message : string ) {
1738 super ( message ) ;
Original file line number Diff line number Diff 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" ;
914import { cleanThreadiverseParams } from "../../helpers" ;
1015import buildSafeClient from "../../SafeClient" ;
16+ import { PiefedErrorResponse } from "../../schemas" ;
1117import {
1218 ListPersonContent ,
1319 ListPersonContentResponse ,
@@ -23,9 +29,13 @@ import { components, paths } from "./schema";
2329async 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
Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change @@ -49,5 +49,7 @@ export * from "./SiteAggregates";
4949export * from "./SubscribedType" ;
5050export * from "./UploadImageResponse" ;
5151
52+ export * from "./PiefedErrorResponse" ;
53+
5254// Should be last (can't access lexical declaration 'PersonView' before initialization)
5355export * from "./list" ;
Original file line number Diff line number Diff line change @@ -122,6 +122,7 @@ export type PersonContentItem = z.infer<typeof schemas.PersonContentItem>;
122122export type PersonMention = z . infer < typeof schemas . PersonMention > ;
123123export type PersonMentionView = z . infer < typeof schemas . PersonMentionView > ;
124124export type PersonView = z . infer < typeof schemas . PersonView > ;
125+ export type PiefedErrorResponse = z . infer < typeof schemas . PiefedErrorResponse > ;
125126export type Post = z . infer < typeof schemas . Post > ;
126127export type PostAggregates = z . infer < typeof schemas . PostAggregates > ;
127128export type PostReport = z . infer < typeof schemas . PostReport > ;
You can’t perform that action at this time.
0 commit comments