grcon/errors.go
Augusto Dwenger J 1cda9ca591
Init commit
Initially it was the idea that this would be version 2 of the
https://github.com/hamburghammer/rcon lib but I changed my mind and
decided to make a new Project out of it.
It has formally nothing to do with the original lib.
2022-02-17 22:33:58 +01:00

94 lines
2.2 KiB
Go

package grcon
import (
"errors"
"fmt"
)
// Action is a type to indicate the part in which an error occurred.
type Action string
// Actions for error checks.
const (
// Write indicates that it happened on a write operation.
Write Action = "write"
// Read indicates that it happened on a read operation.
Read Action = "read"
)
// GrconError is the interface all errors from this packet implement.
// You can use this interface for errors.Is checks.
type GrconError interface {
// Default error interface.
error
// Action returns the action that produced the error.
Action() Action
}
func newGrconGenericError(act Action, err error) GrconGenericError {
return GrconGenericError{
Act: act,
Err: err,
}
}
// GrconGenericError is a generic error that provides default implementations for the grconError interface.
type GrconGenericError struct {
Err error
Act Action
}
// Error returns the error in string format.
func (rge GrconGenericError) Error() string {
return fmt.Sprintf("grcon: on %s: %s", rge.Action(), rge.Err.Error())
}
// Action returns the action where the error was thrown.
func (rge GrconGenericError) Action() Action {
return rge.Act
}
func newUnexpectedFormatError() UnexpectedFormatError {
return UnexpectedFormatError{
newGrconGenericError(
Read,
errors.New("unexpected response format: the packet is smaller than the minimum size"),
),
}
}
// UnexpectedFormatError occurres when the packet size is smaller than the minimum size.
type UnexpectedFormatError struct {
GrconGenericError
}
func newRequestTooLongError() RequestTooLongError {
return RequestTooLongError{
newGrconGenericError(
Write,
errors.New("request body is too long"),
),
}
}
// RequestTooLongError occurres when the body of a packet is to big.
// Can also be an indicator that the package is malformed/corrupted.
type RequestTooLongError struct {
GrconGenericError
}
func newResponseTooLongError() ResponseTooLongError {
return ResponseTooLongError{
newGrconGenericError(
Read,
errors.New("response body is too long"),
),
}
}
// ResponseTooLongError occurres when the size of a packet is to big.
// This indicates a wrongly composed/formatted packet.
type ResponseTooLongError struct {
GrconGenericError
}