Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

request_types: ValidationError -> ErrorValidation #37

Merged
merged 1 commit into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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", ErrorValidation)
}

// 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",
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,
))
)
}
}

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, ErrorValidation))
} else {
assert.NoError(t, err)
}
Expand Down
17 changes: 8 additions & 9 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 @@ -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`
Expand Down
Loading