errcode

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 19, 2023 License: MIT Imports: 3 Imported by: 0

README

errcode

Extends go errors via interfaces to have:

  • unique code (to understand its type)
  • HTTP status code (to respond)

Install

go get github.com/toanppp/errcode

Documentation

https://pkg.go.dev/github.com/toanppp/errcode

Example

package main

import (
	"fmt"
	"github.com/toanppp/errcode"
	"net/http"
)

// Define error type with HTTP status code, message format and its unique code

var (
	InValidError = errcode.NewError(1001, http.StatusBadRequest, "Invalid %v")
)

func Repository() error {
	// Throw error on Repository
	return InValidError.WithArgs("username")
}

func Service() error {
	if err := Repository(); err != nil {
		// Wrap error in many times (on many functions) for easy tracking
		return fmt.Errorf("an error orcured in Repository: %w", err)
	}

	return nil
}

func main() {
	if err := Service(); err != nil {
		// Unwrap to get code, http status code and message to respond to the client
		if ec, ok := errcode.HardUnwrap(err); ok {
			fmt.Println(ec.Code())
			fmt.Println(ec.HTTPStatusCode())
			fmt.Println(ec.Message())
		}
	}
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Unwrap

func Unwrap(err error) error

Unwrap recursion unwraps an error and returns *errcode.Error

Unwrap returns inputted error if not found any *errcode.Error

Example
// Define error
ec := NewError(4, http.StatusBadRequest, "Invalid %v")

// Wrap error in many times (on many functions) for easy tracking
err := fmt.Errorf("service: %w", fmt.Errorf("repo: %w", ec.WithArgs("username")))

// Unwrap to get code, http status code and message to respond to the client
u := Unwrap(err)

fmt.Println(err)
fmt.Println(u)
Output:

service: repo: 4 - Invalid username
4 - Invalid username

Types

type Error

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

func HardUnwrap

func HardUnwrap(err error) (*Error, bool)

HardUnwrap is errcode.Unwrap but returns *errcode.Error instead of error

Example
// Define error
ec := NewError(5, http.StatusBadRequest, "Invalid %v")

// Wrap error in many times (on many functions) for easy tracking
err := fmt.Errorf("service: %w", fmt.Errorf("repo: %w", ec.WithArgs("username")))

// Unwrap to get code, http status code and message to respond to the client
u, ok := HardUnwrap(err)

fmt.Println(err)
fmt.Println(u)
fmt.Println(ok)
fmt.Println(u.Code())
fmt.Println(u.HTTPStatusCode())
fmt.Println(u.Message())
Output:

service: repo: 5 - Invalid username
5 - Invalid username
true
5
400
Invalid username

func NewError

func NewError(code, httpStatusCode int, message string, args ...any) *Error

NewError create new *errcode.Error with unique code

NewError panic if found duplicate code

Example
err := NewError(1, http.StatusInternalServerError, "Internal Server Error")
fmt.Println(err)
Output:

1 - Internal Server Error

func (*Error) Code

func (e *Error) Code() int

func (*Error) Error

func (e *Error) Error() string

func (*Error) HTTPStatusCode

func (e *Error) HTTPStatusCode() int
Example
err := NewError(2, http.StatusNotFound, "Not Found")
fmt.Println(err.HTTPStatusCode())
Output:

404

func (*Error) Message

func (e *Error) Message() string
Example
err := NewError(3, http.StatusBadRequest, "Invalid %v", "username")
fmt.Println(err.Message())
Output:

Invalid username

func (*Error) WithArgs

func (e *Error) WithArgs(args ...any) *Error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL