2022-02-17 21:49:06 +01:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hamburghammer/grcon"
|
|
|
|
)
|
|
|
|
|
2022-03-04 14:01:54 +01:00
|
|
|
func newGrconClientError(act grcon.Action, err error) GrconClientError {
|
|
|
|
return GrconClientError{
|
2022-02-17 21:49:06 +01:00
|
|
|
Act: act,
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-04 14:01:54 +01:00
|
|
|
// GrconClientError is a generic error that provides default implementations for the GrconError interface in the util module.
|
|
|
|
type GrconClientError struct {
|
2022-02-17 21:49:06 +01:00
|
|
|
Err error
|
|
|
|
Act grcon.Action
|
|
|
|
}
|
|
|
|
|
2022-03-04 14:01:54 +01:00
|
|
|
func (grue GrconClientError) Error() string {
|
2022-02-17 21:49:06 +01:00
|
|
|
return fmt.Sprintf("grcon-client: on %s: %s", grue.Action(), grue.Err.Error())
|
|
|
|
}
|
|
|
|
|
2022-03-04 14:01:54 +01:00
|
|
|
func (grue GrconClientError) Action() grcon.Action {
|
2022-02-17 21:49:06 +01:00
|
|
|
return grue.Act
|
|
|
|
}
|
|
|
|
|
|
|
|
func newInvalidResponseTypeError(expected, actual grcon.PacketType) InvalidResponseTypeError {
|
|
|
|
return InvalidResponseTypeError{
|
2022-03-04 14:01:54 +01:00
|
|
|
GrconClientError: newGrconClientError(grcon.Read, fmt.Errorf("invalid response type: expected %d but got %d", expected, actual)),
|
|
|
|
Expected: expected,
|
|
|
|
Actual: actual,
|
2022-02-17 21:49:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type InvalidResponseTypeError struct {
|
2022-03-04 14:01:54 +01:00
|
|
|
GrconClientError
|
2022-02-17 21:49:06 +01:00
|
|
|
Expected grcon.PacketType
|
|
|
|
Actual grcon.PacketType
|
|
|
|
}
|
|
|
|
|
|
|
|
func newAuthFailedError() AuthFailedError {
|
|
|
|
return AuthFailedError{
|
2022-03-04 14:01:54 +01:00
|
|
|
newGrconClientError(grcon.Read, errors.New("authentication failed")),
|
2022-02-17 21:49:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type AuthFailedError struct {
|
2022-03-04 14:01:54 +01:00
|
|
|
GrconClientError
|
2022-02-17 21:49:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func newResponseIdMismatchError(expected, actual grcon.PacketId) ResponseIdMismatchError {
|
|
|
|
return ResponseIdMismatchError{
|
2022-03-04 14:01:54 +01:00
|
|
|
GrconClientError: newGrconClientError(grcon.Read, errors.New("invalid response type")),
|
|
|
|
Expected: expected,
|
|
|
|
Actual: actual,
|
2022-02-17 21:49:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ResponseIdMismatchError struct {
|
2022-03-04 14:01:54 +01:00
|
|
|
GrconClientError
|
2022-02-17 21:49:06 +01:00
|
|
|
Expected grcon.PacketId
|
|
|
|
Actual grcon.PacketId
|
|
|
|
}
|
|
|
|
|
|
|
|
func newResponseBodyError(expected, actual string) ResponseBodyError {
|
|
|
|
return ResponseBodyError{
|
2022-03-04 14:01:54 +01:00
|
|
|
newGrconClientError(
|
2022-02-17 21:49:06 +01:00
|
|
|
grcon.Read,
|
|
|
|
fmt.Errorf("response body error: expected '%s' got '%s'", expected, actual),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ResponseBodyError struct {
|
2022-03-04 14:01:54 +01:00
|
|
|
GrconClientError
|
2022-02-17 21:49:06 +01:00
|
|
|
}
|