generated from mrz1836/go-template
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from pawellewandowski98/feat-104-error-handlimg
feat(BUX-104): update bux error handling
- Loading branch information
Showing
14 changed files
with
340 additions
and
278 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,72 @@ | ||
package transports | ||
|
||
import "errors" | ||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
// ErrAdminKey admin key not set | ||
var ErrAdminKey = errors.New("an admin key must be set to be able to create an xpub") | ||
|
||
// ErrNoClientSet is when no client is set | ||
var ErrNoClientSet = errors.New("no transport client set") | ||
|
||
// ResError is a struct which contain information about error | ||
type ResError struct { | ||
StatusCode int | ||
Message string | ||
} | ||
|
||
// ResponseError is an interface for error | ||
type ResponseError interface { | ||
Error() string | ||
GetStatusCode() int | ||
} | ||
|
||
// WrapError wraps an error into ResponseError | ||
func WrapError(err error) ResponseError { | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
return &ResError{ | ||
StatusCode: http.StatusInternalServerError, | ||
Message: err.Error(), | ||
} | ||
} | ||
|
||
// WrapResponseError wraps a http response into ResponseError | ||
func WrapResponseError(res *http.Response) ResponseError { | ||
if res == nil { | ||
return nil | ||
} | ||
|
||
var errorMsg string | ||
|
||
err := json.NewDecoder(res.Body).Decode(&errorMsg) | ||
if err != nil { | ||
// if EOF, then body is empty and we return response status as error message | ||
if !errors.Is(err, io.EOF) { | ||
errorMsg = fmt.Sprintf("bux-server error message can't be decoded. Reason: %s", err.Error()) | ||
} | ||
errorMsg = res.Status | ||
} | ||
|
||
return &ResError{ | ||
StatusCode: res.StatusCode, | ||
Message: errorMsg, | ||
} | ||
} | ||
|
||
// Error returns the error message | ||
func (e *ResError) Error() string { | ||
return e.Message | ||
} | ||
|
||
// GetStatusCode returns the status code of error | ||
func (e *ResError) GetStatusCode() int { | ||
return e.StatusCode | ||
} |
Oops, something went wrong.