cerror

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2022 License: MIT Imports: 5 Imported by: 0

README

🥷 CError (Custom Error Handling)

GitHub license Documentation Release Go Report Card

Installation

Via go packages: go get github.com/rozturac/cerror

Usages

Console Applications

Here is a sample CError uses for console applications:

import (
    "github.com/rozturac/cerror"
    "log"
    "strconv"
)

func main() {
    defer func() {
        if recover := recover(); recover != nil {
            if err, ok := recover.(cerror.Error); ok {
            switch err.ErrorType() {
                case cerror.DomainError:
                    log.Fatal(fmt.Sprintf("[%s] %s", err.Code(), err.Error()))
                    break
                case cerror.ApplicationError:
                    log.Fatal(err.ErrorWithTrace())
                    break
                case cerror.BusinessError:
                    log.Fatal(err.Error())
                    break
                }
            }
        }
    }()

    x := "23x"
    y, err := strconv.Atoi(x)
    if err != nil {
        panic(cerror.InvalidCastError(x, y).With(err))
    }
}

DomainError Log message:

[InvalidCastError] Cannot convert the '23x' value to int.

ApplicationError Log message:

[InvalidCastError] Cannot convert the '23x' value to int.
strconv.Atoi: parsing "23x": invalid syntax
[main.main] /Users/rozturac/go/src/github.com/cerror/_examples/cmd/main.go:32

BusinessError Log message:

Cannot convert the '23x' value to int.
Web Service Applications

Here is a sample CError uses for web service applications:

import (
	"github.com/rozturac/cerror"
	"net/http"
)

type ErrorResponse struct {
	Code    string `json:"code"`
	Message string `json:"message"`
}

func main() {
	handler := http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
		panic(cerror.NullReferenceError("testProp"))
	})

	mapper := func(errorCode, message string, httpStatusCode int) interface{} {
		return ErrorResponse{
			Code:    errorCode,
			Message: message,
		}
	}

	http.Handle("/example", cerror.AddErrorHandlingMiddlewareWithMapper(handler, mapper))
	http.ListenAndServe(":8080", nil)
}

Response:

Status  : 400
Body    : { code: "NullReferenceError", message: "testProp is null!" }

License

MIT License

Copyright (c) 2021 Rıdvan ÖZTURAÇ

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error interface {
	ErrorType() ErrorType
	ErrorCode() string
	HttpStatusCode() int
	Error() string
	ErrorWithTrace() string
	With(err error) Error
}

Error defines custom error

func InvalidCastError

func InvalidCastError(from, to interface{}) Error

InvalidCastError Create an instance of Invalid Cast Error with object types

func New

func New(errType ErrorType, message string) Error

New Create an instance of Error with Type and Message

func NewWithErrorCode added in v1.3.0

func NewWithErrorCode(errType ErrorType, message string, errCode string) Error

NewWithErrorCode Create an instance of Error with errType, message, code

func NewWithErrorCodeAndHttpStatusCode added in v1.3.0

func NewWithErrorCodeAndHttpStatusCode(errType ErrorType, message, errorCode string, httpStatusCode int) Error

NewWithErrorCodeAndHttpStatusCode Create an instance of Error with extra errorCode and httpStatusCode

func NewWithHttpStatusCode added in v1.3.0

func NewWithHttpStatusCode(errType ErrorType, message string, httpStatusCode int) Error

NewWithHttpStatusCode Create an instance of Error with errType, message, code

func NullReferenceError added in v1.2.0

func NullReferenceError(fieldName string) Error

NullReferenceError Create an instance of Null Reference Error with object name

type ErrorType

type ErrorType string

ErrorType indicate to error level

const (
	// DomainError contains domain errors
	DomainError ErrorType = "DomainError"
	// BusinessError business domain errors
	BusinessError ErrorType = "BusinessError"
	// ApplicationError contains application errors
	ApplicationError ErrorType = "ApplicationError"
)

Directories

Path Synopsis
_examples
cmd command

Jump to

Keyboard shortcuts

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