...

Package derror

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

Overview ▾

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 USE_STRUCT_ERR=true env. An error code is a uint16 numeric value.

▶︎ Error Type

[ 0 ]000
  ↑ 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.

┌────────────────────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────────┬───────┐
│                    │ Description                                                                                                 │ Code  │
├────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────┼───────┤
│ Not Allowed        │ The operation is not allowed to be performed.                                                               │   1   │
├────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────┼───────┤
│ Not Found          │ Intended entity (such as a room) is not found.                                                              │   4   │
├────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────┼───────┤
│ Invalid Parameters │ Given parameter is not valid.                                                                               │   5   │
├────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────┼───────┤
│ Condition Failed   │ The operation and given parameters do not satisfy conditions to perform the command operation successfully. │   6   │
└────────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────┴───────┘

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.

┌────────┬────────────────────────────────────────────────────────────────────────────────────────────────────┬──────┬────────────────────────┐
│ Status │ Description                                                                                        │ Code │ Code With Struct Error │
├────────┼────────────────────────────────────────────────────────────────────────────────────────────────────┼──────┼────────────────────────┤
│ OK     │ The command was successfully executed.                                                             │ 1    │                        │
├────────┼────────────────────────────────────────────────────────────────────────────────────────────────────┼──────┼────────────────────────┤
│ Bad    │ The command execution failed because of invalid command invocation such as invalid parameters etc. │ 4    │ 40                     │
├────────┼────────────────────────────────────────────────────────────────────────────────────────────────────┼──────┼────────────────────────┤
│ Error  │ The command failed to execute due to server failure.                                               │ 5    │ 50                     │
└────────┴────────────────────────────────────────────────────────────────────────────────────────────────────┴──────┴────────────────────────┘

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)

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.

[IMPORT] 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)

type Error

Error is the custom error data

type Error struct {
    // contains filtered or unexported fields
}

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.