From fe15b78c7f194745e77c93ce710a29dbf2516130 Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Sat, 7 Sep 2024 20:45:51 +0100 Subject: [PATCH] request_types: ValidationError -> ErrorValidation --- request.go | 16 +++++++++------- request_test.go | 4 ++-- request_types.go | 17 ++++++++--------- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/request.go b/request.go index f5edeb1..2578de8 100644 --- a/request.go +++ b/request.go @@ -143,7 +143,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{}, ValidationError("empty response") + return []Response{}, fmt.Errorf("%w: empty response", ErrorValidation) } // commands without parameters will have at least one return @@ -151,22 +151,24 @@ func validateResponse(params []string, response []Response) ([]Response, error) // we have a different number of requests and responses if want != len(response) { - return response, ValidationError(fmt.Sprintf( - "want responses: %d, got: %d, responses: %v", + return response, fmt.Errorf( + "%w: want responses: %d, got: %d, responses: %v", + ErrorValidation, want, len(response), response, - )) + ) } // validate that all responses are ok for i, r := range response { if r != "ok" { - return response, ValidationError(fmt.Sprintf( - "non-ok response from param: %s, response: %s", + return response, fmt.Errorf( + "%w: non-ok response from param: %s, response: %s", + ErrorValidation, params[i], r, - )) + ) } } diff --git a/request_test.go b/request_test.go index 7cc8503..a548e61 100644 --- a/request_test.go +++ b/request_test.go @@ -1,6 +1,7 @@ package hyprland import ( + "errors" "flag" "fmt" "os" @@ -218,8 +219,7 @@ 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) + assert.True(t, errors.Is(err, ErrorValidation)) } else { assert.NoError(t, err) } diff --git a/request_types.go b/request_types.go index 536f6e8..9a6d3e0 100644 --- a/request_types.go +++ b/request_types.go @@ -1,6 +1,9 @@ package hyprland -import "net" +import ( + "errors" + "net" +) // Indicates the version where the structs are up-to-date. const HYPRLAND_VERSION = "0.42.0" @@ -19,14 +22,10 @@ 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) -} +// ErrorValidation is used to return errors from response validation. In some +// cases you may want to ignore those errors, in this case you can use +// [errors.Is] to compare the errors returned with this type. +var ErrorValidation = errors.New("validation error") // Unmarshal structs for requests. // Try to keep struct fields in the same order as the output for `hyprctl -j`