apperr

package
v0.0.0-...-22e38cd Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package apperr defines an error that can distinguish between a client error and a server error. It also defines a function that extracts the defined client or server error from the wrapped error.

Example
package main

import (
	"fmt"
	"log"

	"github.com/takuoki/golib/apperr"
	"google.golang.org/grpc/codes"
)

func main() {

	// Buisiness logic
	err := func(id string) error {
		if id == "" {
			return IDRequired
		}
		return NotFound
	}("id1")

	// Error handling
	e, ok := apperr.Extract(err)
	if !ok {
		e = NewInternalServerError(err)
	}
	if e.Log() != "" {
		log.Println(e.Log())
	}
	resp := struct {
		Code    string
		Message string
	}{
		Code:    e.DetailCode(),
		Message: e.Message(),
	}

	fmt.Printf("%+v", resp)

}

var (
	NotFound   = apperr.NewClientError(codes.NotFound, "E0001", "not found")
	IDRequired = apperr.NewClientError(codes.InvalidArgument, "E0002", "id is required")
)

func NewInternalServerError(err error) apperr.Err {
	return apperr.NewServerError(
		codes.Internal,
		"S0001",
		"internal server error",
		err.Error(),
	)
}
Output:

{Code:E0001 Message:not found}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Err

type Err interface {
	Error() string
	Code() codes.Code
	DetailCode() string
	Message() string
	Log() string
	Type() Type

	HTTPStatus() int
	GRPCError(domain string) error
}

Err is a interface that represents an error.

func Extract

func Extract(err error) (Err, bool)

Extract is a function to extract apperr.Err from an error.

func ExtractFromGRPCError

func ExtractFromGRPCError(err error) (Err, bool)

ExtractFromGRPCError is a function to extract apperr.Err from a gRPC error.

Example
package main

import (
	"errors"
	"fmt"

	"github.com/takuoki/golib/apperr"
	"google.golang.org/grpc/codes"
)

func main() {

	callGrpcAPI := func(e apperr.Err) error {
		return e.GRPCError("domain")
	}

	// Client error
	err := callGrpcAPI(NotFound)
	if e, ok := apperr.ExtractFromGRPCError(err); ok {
		fmt.Printf("Code: %s, DetailCode: %s, Message: %s\n", e.Code(), e.DetailCode(), e.Message())
	}

	// Server error
	err = callGrpcAPI(NewInternalServerError(errors.New("error")))
	if e, ok := apperr.ExtractFromGRPCError(err); ok {
		fmt.Printf("Code: %s, DetailCode: %s, Message: %s\n", e.Code(), e.DetailCode(), e.Message())
	}

	// nil error
	err = nil
	if e, ok := apperr.ExtractFromGRPCError(err); ok {
		fmt.Printf("Code: %s, DetailCode: %s, Message: %s\n", e.Code(), e.DetailCode(), e.Message())
	}

	// non-gRPC error
	err = errors.New("error")
	if e, ok := apperr.ExtractFromGRPCError(err); ok {
		fmt.Printf("Code: %s, DetailCode: %s, Message: %s\n", e.Code(), e.DetailCode(), e.Message())
	}

}

var NotFound = apperr.NewClientError(codes.NotFound, "E0001", "not found")

func NewInternalServerError(err error) apperr.Err {
	return apperr.NewServerError(
		codes.Internal,
		"S0001",
		"internal server error",
		err.Error(),
	)
}
Output:

Code: NotFound, DetailCode: E0001, Message: not found
Code: Internal, DetailCode: S0001, Message: internal server error

func NewClientError

func NewClientError(code codes.Code, detailCode, message string) Err

NewClientError creates new client error. Set the gRPC code to `code`. Set the error detail code (ex. "E0001") that the client can handle to `detailCode`.

func NewServerError

func NewServerError(code codes.Code, detailCode, message, log string) Err

NewServerError creates new server error. Set the gRPC code to `code`. Set the error detail code (ex. "S0001") that the client can handle to `detailCode`.

type Type

type Type int

Type is error type.

const (
	ClientError Type = iota + 1
	ServerError
)

Type list.

func (Type) String

func (t Type) String() string

String returns a string of the error type.

Jump to

Keyboard shortcuts

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