This commit is contained in:
admin 2025-08-07 14:47:32 +02:00
commit 1f211de8ef
8 changed files with 171 additions and 0 deletions

10
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,10 @@
{
"cSpell.words": [
"apihub",
"davecgh",
"difflib",
"gopkg",
"pmezard",
"stretchr"
]
}

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2023 Markus Morgenstern
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.

26
README.md Normal file
View File

@ -0,0 +1,26 @@
# Exception
A little Wrapper around the Go Error th handle inner Errors.
## Usage
```Go
package main
import "fmt"
const (
InvalidArgumentException = exception.NewCustom("InvalidArgument")
)
func main() {
// a Error occurs
nilErr := fmt.Errorf("argument can not be nil")
// use Error in Exception
InvalidArgumentException.With(nilErr)
// print out:
// [InvalidArgument]:
// argument can not be nil
println(InvalidArgumentException.Error())
}
```

47
exception.go Normal file
View File

@ -0,0 +1,47 @@
package exception
import (
"bytes"
"fmt"
"os"
)
type Custom interface {
error
With(innerErr ...error) Custom
}
func NewCustom(message string, innerErr ...error) Custom {
return &custom{
message: message,
innerErrors: innerErr,
}
}
type custom struct {
message string
innerErrors []error
}
func (ex *custom) With(innerErr ...error) Custom {
ex.innerErrors = append(ex.innerErrors, innerErr...)
return ex
}
func (ex *custom) Error() string {
buf := bytes.NewBufferString("")
if len(ex.innerErrors) > 0 {
for _, innerE := range ex.innerErrors {
buf.WriteString(getOsNewLine())
buf.WriteString(innerE.Error())
}
}
return fmt.Sprintf("[%s]:%s", ex.message, buf.String())
}
func getOsNewLine() string {
if fmt.Sprintf("%v", os.PathSeparator) != "/" {
return "\n"
}
return "\r\n"
}

42
exception_test.go Normal file
View File

@ -0,0 +1,42 @@
package exception_test
import (
"fmt"
"testing"
"git.apihub24.de/admin/exception"
"github.com/stretchr/testify/suite"
)
type CustomExceptionTestSuite struct {
suite.Suite
}
func (suite *CustomExceptionTestSuite) TestShouldCreateCustomException() {
ex := exception.NewCustom("fail")
suite.NotNil(ex)
suite.Equal(ex.Error(), "[fail]:")
}
func (suite *CustomExceptionTestSuite) TestShouldAddInnerError() {
ex := exception.NewCustom("fail", fmt.Errorf("innerError"))
suite.NotNil(ex)
suite.Equal(ex.Error(), "[fail]:\ninnerError")
}
func (suite *CustomExceptionTestSuite) TestShouldAddInnerErrors() {
ex := exception.NewCustom("fail", fmt.Errorf("innerError1"), fmt.Errorf("innerError2"))
suite.NotNil(ex)
suite.Equal(ex.Error(), "[fail]:\ninnerError1\ninnerError2")
}
func (suite *CustomExceptionTestSuite) TestShouldAddInnerErrorsAfterCreation() {
ex := exception.NewCustom("fail")
ex.With(fmt.Errorf("innerError1"), fmt.Errorf("innerError2"))
suite.NotNil(ex)
suite.Equal(ex.Error(), "[fail]:\ninnerError1\ninnerError2")
}
func TestCustomExceptionTestSuite(t *testing.T) {
suite.Run(t, new(CustomExceptionTestSuite))
}

11
go.mod Normal file
View File

@ -0,0 +1,11 @@
module git.apihub24.de/admin/exception
go 1.22.0
require github.com/stretchr/testify v1.10.0
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

10
go.sum Normal file
View File

@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

4
makefile Normal file
View File

@ -0,0 +1,4 @@
install:
- go mod tidy
test:
- go test -timeout 30s .\...