-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api: acl bootstrap errors aren't 500
Noticed that ACL endpoints return 500 status code for user errors. This is confusing and can lead to false monitoring alerts. Here, I introduce a concept of RPCCoded errors to be returned by RPC that signal a code in addition to error message. Codes for now match HTTP codes to ease reasoning. ``` $ nomad acl bootstrap Error bootstrapping: Unexpected response code: 500 (ACL bootstrap already done (reset index: 9)) $ nomad acl bootstrap Error bootstrapping: Unexpected response code: 400 (ACL bootstrap already done (reset index: 9)) ```
- Loading branch information
Mahmood Ali
committed
Oct 4, 2019
1 parent
550e1b1
commit c1b3668
Showing
4 changed files
with
101 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package structs | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRPCCodedErrors(t *testing.T) { | ||
cases := []struct { | ||
err error | ||
code int | ||
message string | ||
}{ | ||
{ | ||
NewErrRPCCoded(400, "a test message,here"), | ||
400, | ||
"a test message,here", | ||
}, | ||
{ | ||
NewErrRPCCodedf(500, "a test message,here %s %s", "and,here%s", "second"), | ||
500, | ||
"a test message,here and,here%s second", | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.err.Error(), func(t *testing.T) { | ||
code, msg, ok := CodeFromRPCCodedErr(c.err) | ||
assert.True(t, ok) | ||
assert.Equal(t, c.code, code) | ||
assert.Equal(t, c.message, msg) | ||
}) | ||
} | ||
|
||
negativeCases := []string{ | ||
"random error", | ||
errRPCCodedErrorPrefix, | ||
errRPCCodedErrorPrefix + "123", | ||
errRPCCodedErrorPrefix + "qwer,asdf", | ||
} | ||
for _, c := range negativeCases { | ||
t.Run(c, func(t *testing.T) { | ||
_, _, ok := CodeFromRPCCodedErr(errors.New(c)) | ||
assert.False(t, ok) | ||
}) | ||
} | ||
} |