package derror

import "github.com/Diarkis/diarkis/derror"

Package derror ▷

Diarkis error package allows the server to send byte array encoded error structure that contains an error code and an error message for the client.

Error Code

An error code is sent from the server to the client along with an error message as a string. This is enabled when the server is started with DIARKIS_USE_STRUCT_ERR=true env. An error code is a uint16 numeric value.

▶︎ Error Type

[ 0 ]0000
  ↑ This here describes the type of error.

The first digit is used to describe the type of error. The valid error types are listed below.

DescriptionCode
Not AllowedThe operation is not allowed to be performed.1
Condition FailedThe operation and given parameters do not satisfy conditions to perform the command operation successfully.2
Invalid ParametersAt least one of the given parameters is not valid.3
Not FoundIntended entity (such as a room) is not found.4
Server ErrorThe execution of the command failed due to errors caused by the server.5

Server Response Status

A server response comes with a response status code. There are three status codes as follows:

When DIARKIS_USE_STRUCT_ERR=true env is used, the value of Bad and Error response code changes to indicate the use of struct error.

StatusDescriptionCodeCode With Struct Error
OKThe command was successfully executed.1
BadThe command execution failed because of invalid command invocation such as invalid parameters etc.440
ErrorThe command failed to execute due to server failure.550

Usage

derror is meant to be used as errors sent from the server to the client as a response of a command.

// Create a response error data []byte.
// This error response can be decoded and converted into a struct
// with an error message and error code on the client.
errMessage := err.Error()
// We pass 0 here as an example.
// This value here is reserved for custom error code to further describe error details.
errCode := derror.ConditionFailed(0)
errForResponse := derror.ErrData(errMessage, errCode)

// Send the error response to the client
reliable := true
userData.ServerRespond(errForResponse, ver, cmd, server.Bad, reliable)

Index

Functions

func ConditionFailed

func ConditionFailed(customCode uint16) uint16

ConditionFailed returns an error code as Condition Failed with the custom code ranging from 0 to 5535.

Important

If customCode given exceeds 5535, it will forced to be 5535.

Example:

errorCode := ecode.ErrConditionFailed(100)

// errorCode = 20100
fmt.Println(errorCode)

func ErrData

func ErrData(message string, code uint16) []byte

ErrData returns either a UTF8 string message as a byte array or structured error data as a byte array.

Important

This is meant to be sent to the client.

In order to use structured error data, use DIARKIS_USE_STRUCT_ERR env.

func Internal

func Internal(customCode uint16) uint16

Internal returns an error code as internal server failure with the custom code ranging from 0 to 5535.

Important

If customCode given exceeds 5535, it will forced to be 5535.

Example:

errorCode := ecode.ErrInternal(100)

// errorCode = 50100
fmt.Println(errorCode)

func InvalidParameter

func InvalidParameter(customCode uint16) uint16

InvalidParameter returns an error code as Invalid Parameter with the custom code ranging from 0 to 5535.

Important

If customCode given exceeds 5535, it will forced to be 5535.

Example:

errorCode := ecode.ErrInvalidParameter(100)

// errorCode = 30100
fmt.Println(errorCode)

func IsStructErrEnabled

func IsStructErrEnabled() bool

IsStructErrEnabled returns true if DIARKIS_USE_STRUCT_ERR=true env is used.

func NotAllowed

func NotAllowed(customCode uint16) uint16

NotAllowed returns an error code as Not Allowed with the custom code ranging from 0 to 5535.

Important

If customCode given exceeds 5535, it will forced to be 5535.

Example:

errorCode := ecode.ErrNotAllowed(100)

// errorCode = 10100
fmt.Println(errorCode)

func NotFound

func NotFound(customCode uint16) uint16

NotFound returns an error code as Not Found with the custom code ranging from 0 to 5535.

Important

If customCode given exceeds 5535, it will forced to be 5535.

Example:

errorCode := ecode.ErrNotFound(100)

// errorCode = 40100
fmt.Println(errorCode)

Types

type Error

type Error struct {
}

Error is the custom error data

func New

func New(message string, code uint16) *Error

New creates a new error data.

Error Data Byte Array Structure

┌────────────┬───────────────┐
│ Error Code │ Error Message │
├────────────┼───────────────┤
|   uint16   │     string    │
├────────────┼───────────────┤
|  2 bytes   │    variable   │
└────────────┴───────────────┘

func ToError

func ToError(bytes []byte) *Error

ToError converts error bytes to an error data

func (*Error) Bytes

func (e *Error) Bytes() []byte

Bytes returns the byte array for the client.

Note

Encoded byte array does NOT include error stacktrace.

func (*Error) Code

func (e *Error) Code() uint16

Code returns the error code

func (*Error) Message

func (e *Error) Message() string

Message returns the error message

func (*Error) StackTrace

func (e *Error) StackTrace() string

StackTrace return a string of stacktrace.

Note

Decoded *Error from encoded byte array will have an empty stacktrace string.