Skip to content

Commit

Permalink
Error codes (#7)
Browse files Browse the repository at this point in the history
* start of adding more error codes

* adding more common error codes
  • Loading branch information
actatum authored Aug 3, 2022
1 parent 348bce7 commit 006ce21
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 4 deletions.
31 changes: 28 additions & 3 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ type ErrorCode int

// RPC ErrorCodes.
const (
ErrorCodeUnknown ErrorCode = 0
ErrorCodeInternal ErrorCode = 1
ErrorCodeNotFound ErrorCode = 2
ErrorCodeUnknown ErrorCode = 0
ErrorCodeInternal ErrorCode = 1
ErrorCodeNotFound ErrorCode = 2
ErrorCodeInvalidArgument ErrorCode = 3
ErrorCodeUnimplemented ErrorCode = 4
ErrorCodeUnauthenticated ErrorCode = 5
ErrorCodePermissionDenied ErrorCode = 6
ErrorCodeAlreadyExists ErrorCode = 7
)

func (c ErrorCode) String() string {
Expand All @@ -21,6 +26,16 @@ func (c ErrorCode) String() string {
return "STORMRPC_CODE_INTERNAL"
case ErrorCodeNotFound:
return "STORMRPC_CODE_NOT_FOUND"
case ErrorCodeInvalidArgument:
return "STORMRPC_CODE_INVALID_ARGUMENT"
case ErrorCodeUnimplemented:
return "STORMRPC_CODE_UNIMPLEMENTED"
case ErrorCodeUnauthenticated:
return "STORMRPC_CODE_UNAUTHENTICATED"
case ErrorCodePermissionDenied:
return "STORMRPC_CODE_PERMISSION_DENIED"
case ErrorCodeAlreadyExists:
return "STORMRPC_CODE_ALREADY_EXISTS"
default:
return "STORMRPC_CODE_UNKNOWN"
}
Expand Down Expand Up @@ -71,6 +86,16 @@ func codeFromString(s string) ErrorCode {
return ErrorCodeInternal
case "STORMRPC_CODE_NOT_FOUND":
return ErrorCodeNotFound
case "STORMRPC_CODE_INVALID_ARGUMENT":
return ErrorCodeInvalidArgument
case "STORMRPC_CODE_UNIMPLEMENTED":
return ErrorCodeUnimplemented
case "STORMRPC_CODE_UNAUTHENTICATED":
return ErrorCodeUnauthenticated
case "STORMRPC_CODE_PERMISSION_DENIED":
return ErrorCodePermissionDenied
case "STORMRPC_CODE_ALREADY_EXISTS":
return ErrorCodeAlreadyExists
default:
return ErrorCodeUnknown
}
Expand Down
61 changes: 60 additions & 1 deletion errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,31 @@ func TestErrorCode_String(t *testing.T) {
c: ErrorCodeNotFound,
want: "STORMRPC_CODE_NOT_FOUND",
},
{
name: "invalid argument",
c: ErrorCodeInvalidArgument,
want: "STORMRPC_CODE_INVALID_ARGUMENT",
},
{
name: "unimplemented",
c: ErrorCodeUnimplemented,
want: "STORMRPC_CODE_UNIMPLEMENTED",
},
{
name: "unauthenticated",
c: ErrorCodeUnauthenticated,
want: "STORMRPC_CODE_UNAUTHENTICATED",
},
{
name: "permission denied",
c: ErrorCodePermissionDenied,
want: "STORMRPC_CODE_PERMISSION_DENIED",
},
{
name: "already exists",
c: ErrorCodeAlreadyExists,
want: "STORMRPC_CODE_ALREADY_EXISTS",
},
{
name: "default",
c: 10000,
Expand Down Expand Up @@ -115,7 +140,6 @@ func TestMessageFromErr(t *testing.T) {
args args
want string
}{
// TODO: Add test cases.
{
name: "non stormrpc error",
args: args{
Expand Down Expand Up @@ -170,6 +194,41 @@ func Test_codeFromString(t *testing.T) {
},
want: ErrorCodeNotFound,
},
{
name: "invalid argument",
args: args{
s: "STORMRPC_CODE_INVALID_ARGUMENT",
},
want: ErrorCodeInvalidArgument,
},
{
name: "unimplemented",
args: args{
s: "STORMRPC_CODE_UNIMPLEMENTED",
},
want: ErrorCodeUnimplemented,
},
{
name: "unauthenticated",
args: args{
s: "STORMRPC_CODE_UNAUTHENTICATED",
},
want: ErrorCodeUnauthenticated,
},
{
name: "permission denied",
args: args{
s: "STORMRPC_CODE_PERMISSION_DENIED",
},
want: ErrorCodePermissionDenied,
},
{
name: "already exists",
args: args{
s: "STORMRPC_CODE_ALREADY_EXISTS",
},
want: ErrorCodeAlreadyExists,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 006ce21

Please sign in to comment.