11import type { APIVersion } from "@squarecloud/api-types/v2" ;
22
3- import { existsSync } from "node:fs" ;
4- import { mkdir , writeFile } from "node:fs/promises" ;
53import { SquareCloudAPIError } from "@/structures" ;
64import type {
75 APIEndpoint ,
@@ -27,9 +25,25 @@ export class APIService {
2725 const response = await fetch ( url , init ) . catch ( ( err ) => {
2826 throw new SquareCloudAPIError ( err . code , err . message ) ;
2927 } ) ;
30- const data = await response
31- . json ( )
32- . catch ( ( ) => this . handleParseError ( response ) ) ;
28+
29+ if ( response . status === 413 ) {
30+ throw new SquareCloudAPIError ( "PAYLOAD_TOO_LARGE" , "Payload too large" ) ;
31+ }
32+
33+ if ( response . status === 429 ) {
34+ throw new SquareCloudAPIError (
35+ "RATE_LIMIT_EXCEEDED" ,
36+ "Rate limit exceeded" ,
37+ ) ;
38+ }
39+
40+ if ( response . status === 502 || response . status === 504 ) {
41+ throw new SquareCloudAPIError ( "SERVER_UNAVAILABLE" , "Server unavailable" ) ;
42+ }
43+
44+ const data = await response . json ( ) . catch ( ( ) => {
45+ throw new SquareCloudAPIError ( "CANNOT_PARSE_RESPONSE" , "Try again later" ) ;
46+ } ) ;
3347
3448 if ( ! data || data . status === "error" || ! response . ok ) {
3549 throw new SquareCloudAPIError ( data ?. code || "COMMON_ERROR" ) ;
@@ -69,20 +83,4 @@ export class APIService {
6983
7084 return { url, init } ;
7185 }
72-
73- private async handleParseError ( response : Response ) {
74- const text = await response . text ( ) ;
75- const dir = ".squarecloud/logs/" ;
76- const path = `${ dir } ${ this . userId } -${ Date . now ( ) } .log` ;
77-
78- if ( ! existsSync ( dir ) ) {
79- await mkdir ( dir ) ;
80- }
81- await writeFile ( path , text ) ;
82-
83- throw new SquareCloudAPIError (
84- "CANNOT_PARSE_JSON" ,
85- `Saved request text content to ${ path } ` ,
86- ) ;
87- }
8886}
0 commit comments