diff --git a/request.go b/request.go index ea1fc6b..66e98aa 100644 --- a/request.go +++ b/request.go @@ -137,7 +137,7 @@ func parseResponse(raw RawResponse) (response []Response, err error) { func validateResponse(params []string, response []Response) ([]Response, error) { // Empty response, something went terrible wrong if len(response) == 0 { - return []Response{""}, errors.New("empty response") + return []Response{""}, ValidationError("empty response") } want := len(params) @@ -147,18 +147,22 @@ func validateResponse(params []string, response []Response) ([]Response, error) } // we have a different number of requests and responses if want != len(response) { - return response, fmt.Errorf( + return response, ValidationError(fmt.Sprintf( "want responses: %d, got: %d, responses: %v", want, len(response), response, - ) + )) } // validate that all responses are ok for i, r := range response { if r != "ok" { - return response, fmt.Errorf("non-ok response from param: %s, response: %s", params[i], r) + return response, ValidationError(fmt.Sprintf( + "non-ok response from param: %s, response: %s", + params[i], + r, + )) } } diff --git a/request_test.go b/request_test.go index 146eac2..0c82c93 100644 --- a/request_test.go +++ b/request_test.go @@ -201,6 +201,8 @@ func TestValidateResponse(t *testing.T) { assert.DeepEqual(t, response, tt.want) if tt.wantErr { assert.Error(t, err) + _, ok := err.(ValidationError) + assert.True(t, ok) } else { assert.NoError(t, err) } diff --git a/request_types.go b/request_types.go index a74eddf..ac099f3 100644 --- a/request_types.go +++ b/request_types.go @@ -19,6 +19,15 @@ type RequestClient struct { conn *net.UnixAddr } +// ValidationError is used to return errors from response validation. In some +// cases you may want to ignore those errors, in this case you type check this +// kind of error and ignore it. +type ValidationError string + +func (v ValidationError) Error() string { + return string(v) +} + // Unmarshal structs for requests. // Try to keep struct fields in the same order as the output for `hyprctl -j` // for sanity.