Add API Error Handler (#74)
* Add error handler that converts from rpc.Status errors to Watchtower API errors. * Better testing for ErrorHandler. * Rename testcase and remove empty default switch case. * Change the error status back to int64. * Converting everything to int32s. jsonpb wraps int64s as a string which we dont like. We'll figure out how to use values larger than int64s when it comes up. * Remove special casing for TraceId which isn't needed anymore. * Removing wrappers from error details since we never need to know when they are set or unset from the end user. * Using the helper error functions inside the project service. * Correct usage of hclog, replace panic with Error log. * Adding periods to all API returned errors, correct spelling, fix missed invalid argument error not using helper function. * Change our logged errors to Error instead of Warn. * Add TODO for defining and using our own defined API error codes. * Add TODO to remove internal error messages.
This commit is contained in:
1 parent
b718091419
commit
44152ae63e
12 files changed
+354
-130
No files matched your search
+1
-1
@@ -13,7 +13,7 @@ type Error struct {
|
||||
defaultFields []string
|
||||
|
||||
// The HTTP Status code applicable to this error
|
||||
Status *int64 `json:"status,omitempty"`
|
||||
Status *int32 `json:"status,omitempty"`
|
||||
// An application-specific error string
|
||||
Code *string `json:"code,omitempty"`
|
||||
// A human readable explanation specific to this occurrence of the error
|
||||
|
||||
@@ -222,9 +222,7 @@ func parsePBs() {
|
||||
}
|
||||
|
||||
TAGMODIFY:
|
||||
// TraceId needs to be handled differently because it's a
|
||||
// specific value used by OpenTelemetry
|
||||
st.Fields.List[i].Tag.Value = "`" + strings.Replace(regex.FindString(st.Fields.List[i].Tag.Value), "trace_id", "TraceId", 1) + "`"
|
||||
st.Fields.List[i].Tag.Value = "`" + regex.FindString(st.Fields.List[i].Tag.Value) + "`"
|
||||
}
|
||||
}), inAst)
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package scopes
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
@@ -43,7 +44,32 @@ func TestProjects_Crud(t *testing.T) {
|
||||
existed, apiErr, err = org.DeleteProject(tc.Context(), p)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, existed, "Expected project to not exist when deleted, but it did.")
|
||||
|
||||
// TODO: Error conditions once the proper errors are being returned.
|
||||
// Probably as parallel subtests against the same DB.
|
||||
}
|
||||
|
||||
// TODO: Get better coverage for expected errors and error formats.
|
||||
func TestProject_Errors(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
tc := controller.NewTestController(t, nil)
|
||||
defer tc.Shutdown()
|
||||
ctx := tc.Context()
|
||||
|
||||
client := tc.Client()
|
||||
org := &Organization{
|
||||
Client: client,
|
||||
}
|
||||
createdProj, apiErr, err := org.CreateProject(ctx, &Project{})
|
||||
assert.NoError(err)
|
||||
assert.NotNil(createdProj)
|
||||
assert.Nil(apiErr)
|
||||
|
||||
_, apiErr, err = org.ReadProject(ctx, &Project{Id: "p_doesntexis"})
|
||||
assert.NoError(err)
|
||||
// TODO: Should this be nil instead of just a Project that has no values set
|
||||
assert.NotNil(apiErr)
|
||||
assert.EqualValues(*apiErr.Status, http.StatusNotFound)
|
||||
|
||||
_, apiErr, err = org.ReadProject(ctx, &Project{Id: "invalid id"})
|
||||
assert.NoError(err)
|
||||
assert.NotNil(apiErr)
|
||||
assert.EqualValues(*apiErr.Status, http.StatusBadRequest)
|
||||
}
|
||||
+3
-3
@@ -30,15 +30,15 @@ func StringOrNil(in string) *string {
|
||||
}
|
||||
|
||||
// Int returns the given int64 as an int64 pointer
|
||||
func Int(in int64) *int64 {
|
||||
ret := new(int64)
|
||||
func Int(in int32) *int32 {
|
||||
ret := new(int32)
|
||||
*ret = in
|
||||
return ret
|
||||
}
|
||||
|
||||
// IntOrNil is like Int, but the returned pointer will be nil if the integer is
|
||||
// 0
|
||||
func IntOrNil(in int64) *int64 {
|
||||
func IntOrNil(in int32) *int32 {
|
||||
if in == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user