Go error 타입 연습

w00j00ng351·2023년 2월 1일

Go

목록 보기
1/6
package serr

import "fmt"

var (
	ErrNone      error = nil
	ErrUndefined       = NewError(E_ERROR_UNDEFINED, "undefined")
)

const (
	E_ERROR_NONE uint64 = iota
	E_ERROR_UNDEFINED
)

func NewError(code uint64, message string, args ...interface{}) error {
	if code == 0 {
		return nil
	}
	return &_Error{code: code, message: fmt.Sprintf(message, args...)}
}

func AssignError(code uint64, err error) error {
	if err == nil {
		return nil
	}
	if se, ok := err.(_Error); ok {
		return &_Error{code: code, message: se.message}
	}
	return &_Error{code: code, message: err.Error()}
}

type _Error struct {
	code    uint64
	message string
}

func (e _Error) Error() string {
	return fmt.Sprintf("[%d] %s", e.code, e.message)
}
profile
시간이 만든 코드

0개의 댓글