Skip to content

Commit

Permalink
Merge pull request #7 from thiagokokada/add-validation-error
Browse files Browse the repository at this point in the history
request_types: add ValidationError
  • Loading branch information
thiagokokada authored Jul 25, 2024
2 parents 388ea43 + 6ff0d89 commit 5471468
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
12 changes: 8 additions & 4 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
))
}
}

Expand Down
2 changes: 2 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
9 changes: 9 additions & 0 deletions request_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 5471468

Please sign in to comment.