1
1
import type { APIVersion } from "@squarecloud/api-types/v2" ;
2
2
3
- import { existsSync } from "node:fs" ;
4
- import { mkdir , writeFile } from "node:fs/promises" ;
5
3
import { SquareCloudAPIError } from "@/structures" ;
6
4
import type {
7
5
APIEndpoint ,
@@ -27,9 +25,25 @@ export class APIService {
27
25
const response = await fetch ( url , init ) . catch ( ( err ) => {
28
26
throw new SquareCloudAPIError ( err . code , err . message ) ;
29
27
} ) ;
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
+ } ) ;
33
47
34
48
if ( ! data || data . status === "error" || ! response . ok ) {
35
49
throw new SquareCloudAPIError ( data ?. code || "COMMON_ERROR" ) ;
@@ -69,20 +83,4 @@ export class APIService {
69
83
70
84
return { url, init } ;
71
85
}
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
- }
88
86
}
0 commit comments