Remove "proto:" prefix from bad proto JSON messages

This commit is contained in:
Herman Slatman 2022-04-26 14:01:16 +02:00
parent 6e1f8dd7ab
commit bddd08d4b0
No known key found for this signature in database
GPG key ID: F4D8A44EA0A75A4F
3 changed files with 12 additions and 9 deletions

View file

@ -6,6 +6,7 @@ import (
"errors" "errors"
"io" "io"
"net/http" "net/http"
"strings"
"google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
@ -33,7 +34,9 @@ func ProtoJSON(r io.Reader, m proto.Message) error {
switch err := protojson.Unmarshal(data, m); { switch err := protojson.Unmarshal(data, m); {
case errors.Is(err, proto.Error): case errors.Is(err, proto.Error):
return badProtoJSONError(err.Error()) // trim the proto prefix for the message
s := strings.TrimSpace(strings.TrimPrefix(err.Error(), "proto:"))
return badProtoJSONError(s)
default: default:
return err return err
} }

View file

@ -376,7 +376,7 @@ func TestPolicyAdminResponder_CreateAuthorityPolicy(t *testing.T) {
// a syntax error (in the tests). If the message doesn't start with "proto", // a syntax error (in the tests). If the message doesn't start with "proto",
// we expect a full string match. // we expect a full string match.
if strings.HasPrefix(tc.err.Message, "proto:") { if strings.HasPrefix(tc.err.Message, "proto:") {
assert.True(t, strings.Contains(tc.err.Message, "syntax error")) assert.True(t, strings.Contains(ae.Message, "syntax error"))
} else { } else {
assert.Equals(t, tc.err.Message, ae.Message) assert.Equals(t, tc.err.Message, ae.Message)
} }
@ -634,7 +634,7 @@ func TestPolicyAdminResponder_UpdateAuthorityPolicy(t *testing.T) {
// a syntax error (in the tests). If the message doesn't start with "proto", // a syntax error (in the tests). If the message doesn't start with "proto",
// we expect a full string match. // we expect a full string match.
if strings.HasPrefix(tc.err.Message, "proto:") { if strings.HasPrefix(tc.err.Message, "proto:") {
assert.True(t, strings.Contains(tc.err.Message, "syntax error")) assert.True(t, strings.Contains(ae.Message, "syntax error"))
} else { } else {
assert.Equals(t, tc.err.Message, ae.Message) assert.Equals(t, tc.err.Message, ae.Message)
} }
@ -1081,7 +1081,7 @@ func TestPolicyAdminResponder_CreateProvisionerPolicy(t *testing.T) {
// a syntax error (in the tests). If the message doesn't start with "proto", // a syntax error (in the tests). If the message doesn't start with "proto",
// we expect a full string match. // we expect a full string match.
if strings.HasPrefix(tc.err.Message, "proto:") { if strings.HasPrefix(tc.err.Message, "proto:") {
assert.True(t, strings.Contains(tc.err.Message, "syntax error")) assert.True(t, strings.Contains(ae.Message, "syntax error"))
} else { } else {
assert.Equals(t, tc.err.Message, ae.Message) assert.Equals(t, tc.err.Message, ae.Message)
} }
@ -1292,7 +1292,7 @@ func TestPolicyAdminResponder_UpdateProvisionerPolicy(t *testing.T) {
// a syntax error (in the tests). If the message doesn't start with "proto", // a syntax error (in the tests). If the message doesn't start with "proto",
// we expect a full string match. // we expect a full string match.
if strings.HasPrefix(tc.err.Message, "proto:") { if strings.HasPrefix(tc.err.Message, "proto:") {
assert.True(t, strings.Contains(tc.err.Message, "syntax error")) assert.True(t, strings.Contains(ae.Message, "syntax error"))
} else { } else {
assert.Equals(t, tc.err.Message, ae.Message) assert.Equals(t, tc.err.Message, ae.Message)
} }
@ -1694,7 +1694,7 @@ func TestPolicyAdminResponder_CreateACMEAccountPolicy(t *testing.T) {
// a syntax error (in the tests). If the message doesn't start with "proto", // a syntax error (in the tests). If the message doesn't start with "proto",
// we expect a full string match. // we expect a full string match.
if strings.HasPrefix(tc.err.Message, "proto:") { if strings.HasPrefix(tc.err.Message, "proto:") {
assert.True(t, strings.Contains(tc.err.Message, "syntax error")) assert.True(t, strings.Contains(ae.Message, "syntax error"))
} else { } else {
assert.Equals(t, tc.err.Message, ae.Message) assert.Equals(t, tc.err.Message, ae.Message)
} }
@ -1880,7 +1880,7 @@ func TestPolicyAdminResponder_UpdateACMEAccountPolicy(t *testing.T) {
// a syntax error (in the tests). If the message doesn't start with "proto", // a syntax error (in the tests). If the message doesn't start with "proto",
// we expect a full string match. // we expect a full string match.
if strings.HasPrefix(tc.err.Message, "proto:") { if strings.HasPrefix(tc.err.Message, "proto:") {
assert.True(t, strings.Contains(tc.err.Message, "syntax error")) assert.True(t, strings.Contains(ae.Message, "syntax error"))
} else { } else {
assert.Equals(t, tc.err.Message, ae.Message) assert.Equals(t, tc.err.Message, ae.Message)
} }

View file

@ -430,7 +430,7 @@ func TestHandler_CreateProvisioner(t *testing.T) {
assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"]) assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
if strings.HasPrefix(tc.err.Message, "proto:") { if strings.HasPrefix(tc.err.Message, "proto:") {
assert.True(t, strings.Contains(tc.err.Message, "syntax error")) assert.True(t, strings.Contains(adminErr.Message, "syntax error"))
} else { } else {
assert.Equals(t, tc.err.Message, adminErr.Message) assert.Equals(t, tc.err.Message, adminErr.Message)
} }
@ -1087,7 +1087,7 @@ func TestHandler_UpdateProvisioner(t *testing.T) {
assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"]) assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
if strings.HasPrefix(tc.err.Message, "proto:") { if strings.HasPrefix(tc.err.Message, "proto:") {
assert.True(t, strings.Contains(tc.err.Message, "syntax error")) assert.True(t, strings.Contains(adminErr.Message, "syntax error"))
} else { } else {
assert.Equals(t, tc.err.Message, adminErr.Message) assert.Equals(t, tc.err.Message, adminErr.Message)
} }