Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Move all errors into a singular repository
  • Loading branch information
Xosrov committed Apr 9, 2023
1 parent d05c07f commit 7793765
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 0 deletions.
22 changes: 22 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package pkgerrors

import "fmt"

// BadCommanlineArgument indicates a badly formatted commandline argument
type BadCommanlineArgument struct {
For string
Got string
ExpectedFormat string
}

func (e *BadCommanlineArgument) Error() string {
return fmt.Sprintf("BadCommanlineArgument: for %s, got '%s'; however, the expected format is '%s'", e.For, e.Got, e.ExpectedFormat)
}

func NewBadCommanlineArgument(where string, got string, expectedFormat string) error {
return &BadCommanlineArgument{
For: where,
Got: got,
ExpectedFormat: expectedFormat,
}
}
35 changes: 35 additions & 0 deletions controls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package pkgerrors

import "fmt"

// KeyboardInputError indicates a proble sending keyboard input to OS
type KeyboardInputError struct {
// Refer to https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes
Code int
}

func (e *KeyboardInputError) Error() string {
return fmt.Sprintf("KeyboardInputError: Code %d", e.Code)
}

func NewKeyboardInputError(code int) error {
return &KeyboardInputError{
Code: code,
}
}

// MouseInputError indicates that the sending of mouse input to OS failed
type MouseInputError struct {
// Refer to https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes
Code int
}

func (e *MouseInputError) Error() string {
return fmt.Sprintf("MouseInputError: Code %d", e.Code)
}

func NewMouseInputError(code int) error {
return &MouseInputError{
Code: code,
}
}
20 changes: 20 additions & 0 deletions generic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package pkgerrors

import "fmt"

// NotImplementedError indicates missing implementation for a given input
type NotImplementedError struct {
Where string
Feature string
}

func (e *NotImplementedError) Error() string {
return fmt.Sprintf("NotImplementedError: %s not implemented in %s", e.Feature, e.Where)
}

func NewNotImplementedError(where string, feature string) error {
return &NotImplementedError{
Where: where,
Feature: feature,
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/benu-cloud/benu-errors

go 1.20
51 changes: 51 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package pkgerrors

import "fmt"

// UnsupportedMessageTypeError indicates an unsupported message type has been received
type UnsupportedMessageTypeError struct {
GotMessageType string
}

func (e *UnsupportedMessageTypeError) Error() string {
if e.GotMessageType != "" {
return fmt.Sprintf("UnsupportedMessageTypeError: message type '%s' is unsupported", e.GotMessageType)
}
return "UnsupportedMessageTypeError: unknown message type"
}

func NewUnsupportedMessageTypeError(gotMessageType string) error {
return &UnsupportedMessageTypeError{
GotMessageType: gotMessageType,
}
}

// UnmarshalError indicates error in unmarshaling message
type UnmarshalError struct {
Err error
}

func (e *UnmarshalError) Error() string {
return fmt.Sprintf("UnmarshalError: %v", e.Err)
}

func NewUnmarshalError(err error) error {
return &UnmarshalError{
Err: err,
}
}

// UnmarshalError indicates error in marshaling message
type MarshalError struct {
Err error
}

func (e *MarshalError) Error() string {
return fmt.Sprintf("MarshalError: %v", e.Err)
}

func NewMarshalError(err error) error {
return &MarshalError{
Err: err,
}
}
33 changes: 33 additions & 0 deletions rabbitmq.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package pkgerrors

import "fmt"

// PublishError indicates an error publishing to rabbitmq
type PublishError struct {
Err error
}

func (e *PublishError) Error() string {
return fmt.Sprintf("PublishError: %v", e.Err)
}

func NewPublishError(err error) error {
return &PublishError{
Err: err,
}
}

// ConsumeError indicates an error consuming from rabbitmq
type ConsumeError struct {
Err error
}

func (e *ConsumeError) Error() string {
return fmt.Sprintf("ConsumeError: %v", e.Err)
}

func NewConsumeError(err error) error {
return &ConsumeError{
Err: err,
}
}
61 changes: 61 additions & 0 deletions stream.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package pkgerrors

import "fmt"

// ! MUST MATCH ERROR CODES FROM C CODE
var streamErrorCodes = map[int]string{
1: "ERROR_ENCODER_NOT_SUPPORTED",
2: "ERROR_PIPELINE_ALREADY_CREATED",
3: "ERROR_PIPELINE_PARSE_BAD_FORMAT",
4: "ERROR_PIPELINE_BAD_STATE",
5: "ERROR_PIPELINE_SET_STATE",
6: "ERROR_LINKING_PEER",
8: "ERROR_BAD_PEER_ID",
7: "ERROR_PIPELINE_DOESNT_EXIST",
9: "ERROR_BAD_SDP",
}

// CStreamError indicates an error from the stream C code
type CStreamError struct {
ErrorCode int
ErrorMessage string
}

func (e *CStreamError) Error() string {
if e.ErrorMessage == "" {
errorStr, ok := streamErrorCodes[e.ErrorCode]
if !ok {
return fmt.Sprintf("CStreamError: got unknown error with code %d from stream", e.ErrorCode)
}
return fmt.Sprintf("CStreamError: got error %s with code %d from stream", errorStr, e.ErrorCode)
} else {
return fmt.Sprintf("CStreamError: got error '%s'", e.ErrorMessage)
}
}

func NewCStreamError(errorCode int) error {
return &CStreamError{
ErrorCode: errorCode,
}
}

func NewCStreamErrorWithMessage(errorMessage string) error {
return &CStreamError{
ErrorMessage: errorMessage,
}
}

// StreamError indicates an error from the Go code
type StreamError struct {
Err error
}

func (e *StreamError) Error() string {
return fmt.Sprintf("StreamError: %v", e.Err)
}

func NewStreamError(err error) error {
return &StreamError{
Err: err,
}
}

0 comments on commit 7793765

Please sign in to comment.