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 <[email protected]>
  • Loading branch information
2 people authored and rsempe committed Jun 10, 2024
1 parent 972c92e commit 71e4719
Show file tree
Hide file tree
Showing 3 changed files with 38 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())
}
1 change: 1 addition & 0 deletions invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ type Invoice struct {
IssuingDate string `json:"issuing_date,omitempty"`
PaymentDisputeLostAt time.Time `json:"payment_dispute_lost_at,omitempty"`
PaymentDueDate string `json:"payment_due_date,omitempty"`
PaymentOverdue bool `json:"payment_overdue,omitempty"`

InvoiceType InvoiceType `json:"invoice_type,omitempty"`
Status InvoiceStatus `json:"status,omitempty"`
Expand Down

0 comments on commit 71e4719

Please sign in to comment.