-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This exposes a new public API: ValidateRequest. This function may be used by transport authors to validate that a `transport.Request` is valid. This function DOES NOT validate that a `context.Context` has a timeout set on it. That's a transport-level concern (especially because unary transports don't have timeouts). As for the internal APIs: The `ValidateUnary` and `ValidateOneway` functions have been replaced with `ValidateUnaryContext` and `ValidateOnewayContext`.
- Loading branch information
Showing
12 changed files
with
159 additions
and
228 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,47 @@ | ||
package errors | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// MissingParameters returns an error representing a failure to process a | ||
// request because it was missing required parameters. | ||
func MissingParameters(params []string) error { | ||
if len(params) == 0 { | ||
return nil | ||
} | ||
|
||
return missingParametersError{Parameters: params} | ||
} | ||
|
||
// missingParametersError is a failure to process a request because it was | ||
// missing required parameters. | ||
type missingParametersError struct { | ||
// Names of the missing parameters. | ||
// | ||
// Precondition: len(Parameters) > 0 | ||
Parameters []string | ||
} | ||
|
||
func (e missingParametersError) AsHandlerError() HandlerError { | ||
return HandlerBadRequestError(e) | ||
} | ||
|
||
func (e missingParametersError) Error() string { | ||
s := "missing " | ||
ps := e.Parameters | ||
if len(ps) == 1 { | ||
s += ps[0] | ||
return s | ||
} | ||
|
||
if len(ps) == 2 { | ||
s += fmt.Sprintf("%s and %s", ps[0], ps[1]) | ||
return s | ||
} | ||
|
||
s += strings.Join(ps[:len(ps)-1], ", ") | ||
s += fmt.Sprintf(", and %s", ps[len(ps)-1]) | ||
return s | ||
} |
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,40 @@ | ||
package errors | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMissingParameters(t *testing.T) { | ||
tests := []struct { | ||
params []string | ||
want string | ||
}{ | ||
{}, | ||
{ | ||
[]string{"x"}, | ||
"missing x", | ||
}, | ||
{ | ||
[]string{"x", "y"}, | ||
"missing x and y", | ||
}, | ||
{ | ||
[]string{"x", "y", "z"}, | ||
"missing x, y, and z", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
err := MissingParameters(tt.params) | ||
if tt.want != "" { | ||
if assert.Error(t, err) { | ||
assert.Equal(t, tt.want, err.Error()) | ||
} | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
|
||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.