Skip to content

Latest commit

 

History

History
131 lines (104 loc) · 3.91 KB

README.md

File metadata and controls

131 lines (104 loc) · 3.91 KB

🥷 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.