33 ApiKeyError ,
44 ClientError ,
55 DefaultError ,
6- ErrorResponse ,
6+ isErrorResponse ,
77 NetworkError ,
88 ServerError ,
99} from '../errors/defaultError' ;
@@ -21,27 +21,49 @@ class RetryableError extends Data.TaggedError('RetryableError')<{
2121} > { }
2222
2323const handleOkResponse = < R > ( res : Response ) =>
24- Effect . tryPromise ( {
25- try : async ( ) : Promise < R > => {
26- const responseText = await res . text ( ) ;
24+ pipe (
25+ Effect . tryPromise ( {
26+ try : ( ) => res . text ( ) ,
27+ catch : e =>
28+ new DefaultError ( {
29+ errorCode : 'ParseError' ,
30+ errorMessage : e instanceof Error ? e . message : String ( e ) ,
31+ context : {
32+ responseStatus : res . status ,
33+ responseUrl : res . url ,
34+ } ,
35+ } ) ,
36+ } ) ,
37+ Effect . flatMap ( responseText => {
2738 if ( ! responseText ) {
2839 if ( res . status === 204 ) {
29- return { } as R ;
40+ return Effect . succeed ( { } as R ) ;
3041 }
31- throw new Error ( 'API returned empty response body' ) ;
42+ return Effect . fail (
43+ new DefaultError ( {
44+ errorCode : 'ParseError' ,
45+ errorMessage : 'API returned empty response body' ,
46+ context : {
47+ responseStatus : res . status ,
48+ responseUrl : res . url ,
49+ } ,
50+ } ) ,
51+ ) ;
3252 }
33- return JSON . parse ( responseText ) as R ;
34- } ,
35- catch : e =>
36- new DefaultError ( {
37- errorCode : 'ParseError' ,
38- errorMessage : e instanceof Error ? e . message : String ( e ) ,
39- context : {
40- responseStatus : res . status ,
41- responseUrl : res . url ,
42- } ,
43- } ) ,
44- } ) ;
53+ return Effect . try ( {
54+ try : ( ) => JSON . parse ( responseText ) as R ,
55+ catch : e =>
56+ new DefaultError ( {
57+ errorCode : 'ParseError' ,
58+ errorMessage : e instanceof Error ? e . message : String ( e ) ,
59+ context : {
60+ responseStatus : res . status ,
61+ responseUrl : res . url ,
62+ } ,
63+ } ) ,
64+ } ) ;
65+ } ) ,
66+ ) ;
4567
4668const handleClientErrorResponse = ( res : Response ) =>
4769 pipe (
@@ -65,52 +87,57 @@ const handleClientErrorResponse = (res: Response) =>
6587 url : res . url ,
6688 } ) ;
6789
68- let json : Partial < ErrorResponse > ;
69- try {
70- json = JSON . parse ( text ) as Partial < ErrorResponse > ;
71- } catch {
72- return Effect . fail ( genericError ) ;
73- }
74-
75- return Effect . fail (
76- json != null && json . errorCode && json . errorMessage
77- ? new ClientError ( {
78- errorCode : json . errorCode ,
79- errorMessage : json . errorMessage ,
80- httpStatus : res . status ,
81- url : res . url ,
82- } )
83- : genericError ,
90+ return pipe (
91+ Effect . try ( {
92+ try : ( ) => JSON . parse ( text ) as unknown ,
93+ catch : ( ) => genericError ,
94+ } ) ,
95+ Effect . flatMap ( json =>
96+ Effect . fail (
97+ isErrorResponse ( json )
98+ ? new ClientError ( {
99+ errorCode : json . errorCode ,
100+ errorMessage : json . errorMessage ,
101+ httpStatus : res . status ,
102+ url : res . url ,
103+ } )
104+ : genericError ,
105+ ) ,
106+ ) ,
84107 ) ;
85108 } ) ,
86109 ) ;
87110
88111/**
89- * JSON 파싱을 시도하여 적절한 ServerError를 결정하는 순수 함수 .
90- * 모든 경로가 ServerError를 반환한다 (서버 에러 응답이므로 성공 경로 없음).
112+ * JSON 파싱을 시도하여 적절한 ServerError로 실패하는 Effect를 반환 .
113+ * 모든 경로가 ServerError로 실패한다 (서버 에러 응답이므로 성공 경로 없음).
91114 */
92115function parseServerErrorBody (
93116 text : string ,
94117 genericError : ServerError ,
95118 makeError : ( errorCode : string , errorMessage : string ) => ServerError ,
96- ) : ServerError {
97- let json : Partial < ErrorResponse > ;
98- try {
99- json = JSON . parse ( text ) as Partial < ErrorResponse > ;
100- } catch ( parseError ) {
101- if ( parseError instanceof SyntaxError ) {
102- return genericError ;
103- }
104- return makeError (
105- 'ResponseParseError' ,
106- parseError instanceof Error ? parseError . message : String ( parseError ) ,
107- ) ;
108- }
109-
110- if ( json != null && json . errorCode && json . errorMessage ) {
111- return makeError ( json . errorCode , json . errorMessage ) ;
112- }
113- return genericError ;
119+ ) : Effect . Effect < never , ServerError > {
120+ return pipe (
121+ Effect . try ( {
122+ try : ( ) => JSON . parse ( text ) as unknown ,
123+ catch : ( parseError : unknown ) =>
124+ parseError instanceof SyntaxError
125+ ? genericError
126+ : makeError (
127+ 'ResponseParseError' ,
128+ parseError instanceof Error
129+ ? parseError . message
130+ : String ( parseError ) ,
131+ ) ,
132+ } ) ,
133+ Effect . flatMap ( json =>
134+ Effect . fail (
135+ isErrorResponse ( json )
136+ ? makeError ( json . errorCode , json . errorMessage )
137+ : genericError ,
138+ ) ,
139+ ) ,
140+ ) ;
114141}
115142
116143const handleServerErrorResponse = ( res : Response ) =>
@@ -146,7 +173,7 @@ const handleServerErrorResponse = (res: Response) =>
146173 text . substring ( 0 , 200 ) || 'Server error occurred' ,
147174 ) ;
148175
149- return Effect . fail ( parseServerErrorBody ( text , genericError , makeError ) ) ;
176+ return parseServerErrorBody ( text , genericError , makeError ) ;
150177 } ) ,
151178 ) ;
152179
0 commit comments