Skip to content

rozturac/cerror

Repository files navigation

🥷 CError (Custom Error Handling)

GitHub license Documentation Release Go Report Card

Installation

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

Usage

Here is a sample CError uses:

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

func main() {
    defer func() {
        if recover := recover(); recover != nil {
            if err, ok := recover.(cerror.Error); ok {
                log.Fatal(err.ErrorWithTrace())
            }
        }
    }()
    
    x := "23x"
    y, err := strconv.Atoi(x)
    if err != nil {
        panic(cerror.InvalidCastError(x, y).With(err))
    }
}

Log message:

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

Console message:

Cannot convert the '23x' value to a int.