-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
110 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package errors | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"encoding/gob" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
type Error struct { | ||
StatusCode int16 | ||
Detail string | ||
} | ||
|
||
func NewError(code int16, format string, a ...interface{}) error { | ||
return &Error{ | ||
StatusCode: code, | ||
Detail: fmt.Sprintf(format, a...), | ||
} | ||
} | ||
|
||
// Encode transforms the error instance into a base64-encoded blob | ||
func (e *Error) Encode() (string, error) { | ||
var b bytes.Buffer | ||
enc := gob.NewEncoder(&b) | ||
|
||
if err := enc.Encode(e); err != nil { | ||
return "", err | ||
} | ||
|
||
return base64.StdEncoding.EncodeToString(b.Bytes()), nil | ||
} | ||
|
||
// Decode tries to decode en error from a base64-encoded string | ||
func Decode(str string) (*Error, error) { | ||
b := strings.NewReader(str) | ||
dec := gob.NewDecoder(b) | ||
|
||
var merr Error | ||
if err := dec.Decode(&merr); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &merr, nil | ||
} | ||
|
||
func (e *Error) Error() string { | ||
return fmt.Sprintf("%s: %s", http.StatusText(int(e.StatusCode)), e.Detail) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package errors | ||
|
||
func BadRequest(format string, a ...string) error { | ||
return NewError(400, format, a) | ||
} | ||
|
||
func Unauthorized(format string, a ...string) error { | ||
return NewError(401, format, a) | ||
} | ||
|
||
func Forbidden(format string, a ...string) error { | ||
return NewError(403, format, a) | ||
} | ||
|
||
func NotFound(format string, a ...string) error { | ||
return NewError(404, format, a) | ||
} | ||
|
||
func InternalServerError(format string, a ...string) error { | ||
return NewError(404, format, a) | ||
} |
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