package protocol

import "errors"

type ErrorCode int

const (
	E_PARSE            ErrorCode = -32700
	E_INVALID_REQ      ErrorCode = -32600
	E_NOT_FOUND_METHOD ErrorCode = -32601
	E_INVALID_PARAMS   ErrorCode = -32602
	E_INTERNAL         ErrorCode = -32603
	E_SERVER           ErrorCode = -32000
)

var ErrNullResult = errors.New("result is null")

type Error struct {
	Code    ErrorCode   `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"`
}

type Handler interface {
	Handle([]byte) ([]byte, *Error)
}

func NewError(code ErrorCode, err error, data interface{}) *Error {
	e := &Error{
		Code:    code,
		Message: err.Error(),
		Data:    data,
	}
	return e
}
