@@ -127,7 +127,7 @@ func writePass(pwFileName string, pwFileContent string) error {
127
127
return nil
128
128
}
129
129
130
- func createCOSCSIMounterRequest (payload string , url string ) ( string , error ) {
130
+ func createCOSCSIMounterRequest (payload string , url string ) error {
131
131
// Get socket path
132
132
socketPath := os .Getenv (constants .COSCSIMounterSocketPathEnv )
133
133
if socketPath == "" {
@@ -137,7 +137,7 @@ func createCOSCSIMounterRequest(payload string, url string) (string, error) {
137
137
138
138
err := isGRPCServerAvailable (socketPath )
139
139
if err != nil {
140
- return "" , err
140
+ return err
141
141
}
142
142
143
143
// Create a custom dialer function for Unix socket connection
@@ -156,12 +156,12 @@ func createCOSCSIMounterRequest(payload string, url string) (string, error) {
156
156
// Create POST request
157
157
req , err := http .NewRequest ("POST" , url , strings .NewReader (payload ))
158
158
if err != nil {
159
- return "" , err
159
+ return err
160
160
}
161
161
req .Header .Set ("Content-Type" , "application/json" )
162
162
response , err := client .Do (req )
163
163
if err != nil {
164
- return "" , err
164
+ return err
165
165
}
166
166
167
167
defer func () {
@@ -172,40 +172,42 @@ func createCOSCSIMounterRequest(payload string, url string) (string, error) {
172
172
173
173
body , err := io .ReadAll (response .Body )
174
174
if err != nil {
175
- return "" , err
175
+ return err
176
176
}
177
177
178
178
responseBody := string (body )
179
179
klog .Infof ("response from cos-csi-mounter -> Response body: %s, Response code: %v" , responseBody , response .StatusCode )
180
180
181
181
if response .StatusCode != http .StatusOK {
182
- return responseBody , fetchGRPCReturnCode (response .StatusCode )
182
+ return parseGRPCResponse (response .StatusCode , responseBody )
183
183
}
184
- return "" , nil
184
+ return nil
185
185
}
186
186
187
- func fetchGRPCReturnCode (code int ) error {
187
+ // parseGRPCResponse takes both response body and error code and frames error message
188
+ func parseGRPCResponse (code int , response string ) error {
189
+ errMsg := parseErrFromResponse (response )
188
190
switch code {
189
191
case http .StatusBadRequest :
190
- return status .Error (codes .InvalidArgument , "Invalid Argument" )
192
+ return status .Error (codes .InvalidArgument , errMsg )
191
193
case http .StatusNotFound :
192
- return status .Error (codes .NotFound , "Not Found" )
194
+ return status .Error (codes .NotFound , errMsg )
193
195
case http .StatusConflict :
194
- return status .Error (codes .AlreadyExists , "Already Exists" )
196
+ return status .Error (codes .AlreadyExists , errMsg )
195
197
case http .StatusForbidden :
196
- return status .Error (codes .PermissionDenied , "Permission Denied" )
198
+ return status .Error (codes .PermissionDenied , errMsg )
197
199
case http .StatusTooManyRequests :
198
- return status .Error (codes .ResourceExhausted , "Resource Exhausted" )
200
+ return status .Error (codes .ResourceExhausted , errMsg )
199
201
case http .StatusNotImplemented :
200
- return status .Error (codes .Unimplemented , "Unimplemented" )
202
+ return status .Error (codes .Unimplemented , errMsg )
201
203
case http .StatusInternalServerError :
202
- return status .Error (codes .Internal , "Internal" )
204
+ return status .Error (codes .Internal , errMsg )
203
205
case http .StatusServiceUnavailable :
204
- return status .Error (codes .Unavailable , "Unavailable" )
206
+ return status .Error (codes .Unavailable , errMsg )
205
207
case http .StatusUnauthorized :
206
- return status .Error (codes .Unauthenticated , "Unauthenticated" )
208
+ return status .Error (codes .Unauthenticated , errMsg )
207
209
default :
208
- return status .Error (codes .Unknown , "Unknown" )
210
+ return status .Error (codes .Unknown , errMsg )
209
211
}
210
212
}
211
213
@@ -222,16 +224,19 @@ func isGRPCServerAvailable(socketPath string) error {
222
224
return nil
223
225
}
224
226
225
- func parseErrFromResponse (response string ) error {
227
+ // parseErrFromResponse fetches error from responseBody
228
+ // e.g. ResponseBody: {"error":"invalid args for mounter: invalid s3fs args decode error: json: unknown field \"unknownkey\""}
229
+ // parseErrFromResponse returns "invalid args for mounter: invalid s3fs args decode error: json: unknown field \"unknownkey\"
230
+ func parseErrFromResponse (response string ) string {
226
231
var errFromResp map [string ]string
227
232
err := json .Unmarshal ([]byte (response ), & errFromResp )
228
233
if err != nil {
229
- klog .Warning ("failed to unmarshal response from server" )
230
- return errors . New ( response )
234
+ klog .Warning ("failed to unmarshal response from server" , err )
235
+ return response
231
236
}
232
237
val , exists := errFromResp ["error" ]
233
238
if ! exists {
234
- return errors . New ( response )
239
+ return response
235
240
}
236
- return errors . New ( val )
241
+ return val
237
242
}
0 commit comments