Skip to content

Commit

Permalink
feat: implement error interface (#179)
Browse files Browse the repository at this point in the history
Co-authored-by: Jordan Wu <101218661+jordan-definitive@users.noreply.github.com>
  • Loading branch information
jordan-wu-97 and jordan-wu-97 authored Jun 6, 2024
1 parent 972c92e commit 0b29a3a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
14 changes: 14 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lago

import (
"encoding/json"
"errors"
"net/http"
)
Expand Down Expand Up @@ -28,6 +29,19 @@ type Error struct {
ErrorDetail map[string][]string `json:"error_details,omitempty"`
}

func (e Error) Error() string {
type alias struct {
Error
Err string `json:"err,omitempty"`
}
err := alias{Error: e}
if e.Err != nil {
err.Err = e.Err.Error()
}
msg, _ := json.Marshal(&err)
return string(msg)
}

func (e ErrorCode) Error() string {
return string(e)
}
23 changes: 23 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package lago

import (
"errors"
"testing"
)

func TestErrorErr(t *testing.T) {
var hasErr error = Error{
Err: errors.New("type assertion failed"),
HTTPStatusCode: 422,
Message: "Type assertion failed",
}
t.Logf("%s", hasErr.Error())
}

func TestErrorNoErr(t *testing.T) {
var noErr error = Error{
HTTPStatusCode: 500,
Message: "500",
}
t.Logf("%s", noErr.Error())
}

0 comments on commit 0b29a3a

Please sign in to comment.