Skip to content

Commit 53eab64

Browse files
author
Aryan Tikarya
committed
add error code in interface to get custom codes
1 parent a0806e5 commit 53eab64

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

errors.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ type marshalable interface {
5959
json.Unmarshaler
6060
}
6161

62+
// Error wraps RPC errors, which contain an error code in addition to the message.
63+
type Error interface {
64+
Error() string // returns the message
65+
ErrorCode() int // returns the code
66+
}
67+
68+
// DataError contains extra data to explain the error
6269
type DataError interface {
6370
Error() string // returns the message
6471
ErrorData() interface{} // returns the error data

handler.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,13 @@ func (s *handler) createError(err error) *respError {
365365
}
366366
}
367367

368-
if de, ok := err.(DataError); ok {
368+
var err2 Error
369+
if errors.As(err, &err2) {
370+
out.Code = ErrorCode(err2.ErrorCode())
371+
}
372+
373+
var de DataError
374+
if errors.As(err, &de) {
369375
out.Data = de.ErrorData()
370376
}
371377

0 commit comments

Comments
 (0)