Skip to content

Commit 739ed23

Browse files
committed
Expose more useful http error info
Use a custom error type so that the error status and body can be read from the calling code. The error gets wrapped so you'll need to use errors.As() to get the body.
1 parent f32315e commit 739ed23

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

error.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func CreateChartTimeError() error {
3030
// CreateRemoteError returns an error
3131
// with a message about a remote api problem.
3232
func CreateRemoteError(e error) error {
33-
return fmt.Errorf("code: %s, detail: %s", remoteErrorCode, e.Error())
33+
return fmt.Errorf("code: %s, detail: %w", remoteErrorCode, e)
3434
}
3535

3636
// CreateRemoteErrorS returns an error

finance.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package finance
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"io/ioutil"
78
"log"
89
"net/http"
@@ -226,7 +227,11 @@ func (s *BackendConfiguration) Do(req *http.Request, v interface{}) error {
226227
if LogLevel > 0 {
227228
Logger.Printf("API error: %q\n", resBody)
228229
}
229-
return CreateRemoteErrorS("error response recieved from upstream api")
230+
return &RemoteError{
231+
Msg: "error response recieved from upstream api",
232+
StatusCode: res.StatusCode,
233+
Body: string(resBody),
234+
}
230235
}
231236

232237
if LogLevel > 2 {
@@ -239,3 +244,13 @@ func (s *BackendConfiguration) Do(req *http.Request, v interface{}) error {
239244

240245
return nil
241246
}
247+
248+
type RemoteError struct {
249+
Msg string
250+
StatusCode int
251+
Body string
252+
}
253+
254+
func (e *RemoteError) Error() string {
255+
return fmt.Sprintf("status: %d, detail: %s", e.StatusCode, e.Msg)
256+
}

0 commit comments

Comments
 (0)