Skip to content

Commit

Permalink
request_test: ValidationError should be an errors.New
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagokokada committed Sep 7, 2024
1 parent 2636df5 commit 2c08a62
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
16 changes: 9 additions & 7 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,30 +143,32 @@ 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", ValidationError)
}

// commands without parameters will have at least one return
want := max(len(params), 1)

// 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",
ValidationError,
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",
ValidationError,
params[i],
r,
))
)
}
}

Expand Down
4 changes: 2 additions & 2 deletions request_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hyprland

import (
"errors"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -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, ValidationError))
} else {
assert.NoError(t, err)
}
Expand Down
11 changes: 5 additions & 6 deletions request_types.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -22,11 +25,7 @@ type RequestClient struct {
// 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)
}
var ValidationError = errors.New("validation error")

Check failure on line 28 in request_types.go

View workflow job for this annotation

GitHub Actions / build

error var ValidationError should have name of the form ErrFoo (ST1012)

// Unmarshal structs for requests.
// Try to keep struct fields in the same order as the output for `hyprctl -j`
Expand Down

0 comments on commit 2c08a62

Please sign in to comment.