Skip to content

Commit

Permalink
Add errors package and handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Jan 21, 2021
1 parent b3215a7 commit 4a282ff
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 1 deletion.
4 changes: 4 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ func (c *client) Call(service string, path string, reqval interface{}, respval i
return fmt.Errorf("decode response: %w", err)
}

if err, ok := respmsg.GetError(); ok {
return err
}

return nil
}

Expand Down
51 changes: 51 additions & 0 deletions errors/error.go
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)
}
21 changes: 21 additions & 0 deletions errors/errors.go
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)
}
35 changes: 34 additions & 1 deletion transport/message.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package transport

import (
goerrors "errors"

"github.com/MouseHatGames/mice/errors"
)

type Message struct {
Headers map[string]string
Data []byte
Expand All @@ -12,5 +18,32 @@ func NewMessage() *Message {
}

func (m *Message) SetError(err error) {
m.Headers[HeaderError] = err.Error()
var value string

if merr, ok := err.(*errors.Error); ok {
enc, err := merr.Encode()

if err != nil {
value = err.Error()
} else {
value = enc
}
} else {
value = err.Error()
}

m.Headers[HeaderError] = value
}

func (m *Message) GetError() (err error, hasError bool) {
value, ok := m.Headers[HeaderError]
if !ok {
return nil, false
}

if merr, err := errors.Decode(value); err != nil {
return merr, true
}

return goerrors.New(value), true
}

0 comments on commit 4a282ff

Please sign in to comment.