From 1f211de8ef04ef1d6372e7b01d71c0540a6bc46b Mon Sep 17 00:00:00 2001 From: admin Date: Thu, 7 Aug 2025 14:47:32 +0200 Subject: [PATCH] init --- .vscode/settings.json | 10 +++++++++ LICENSE | 21 +++++++++++++++++++ README.md | 26 ++++++++++++++++++++++++ exception.go | 47 +++++++++++++++++++++++++++++++++++++++++++ exception_test.go | 42 ++++++++++++++++++++++++++++++++++++++ go.mod | 11 ++++++++++ go.sum | 10 +++++++++ makefile | 4 ++++ 8 files changed, 171 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 LICENSE create mode 100644 README.md create mode 100644 exception.go create mode 100644 exception_test.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 makefile diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..de28925 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,10 @@ +{ + "cSpell.words": [ + "apihub", + "davecgh", + "difflib", + "gopkg", + "pmezard", + "stretchr" + ] +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9deaeee --- /dev/null +++ b/LICENSE @@ -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. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..ab9820b --- /dev/null +++ b/README.md @@ -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()) +} +``` diff --git a/exception.go b/exception.go new file mode 100644 index 0000000..51ea232 --- /dev/null +++ b/exception.go @@ -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" +} diff --git a/exception_test.go b/exception_test.go new file mode 100644 index 0000000..7a975c6 --- /dev/null +++ b/exception_test.go @@ -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)) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..cc9efa7 --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..713a0b4 --- /dev/null +++ b/go.sum @@ -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= diff --git a/makefile b/makefile new file mode 100644 index 0000000..b19eb1d --- /dev/null +++ b/makefile @@ -0,0 +1,4 @@ +install: + - go mod tidy +test: + - go test -timeout 30s .\... \ No newline at end of file