Erx provides a new way to create errors in go, embed kind, severity, operation and cause of the error in a single object. You can stack erx.
erx.WithArgs(errors.New("some error"))
prebuilt kind
erx.WithArgs(errors.New("some error"), erx.ValidationError)
custom
erx.WithArgs(errors.New("some error"), erx.Kind("DependencyError"))
erx.WithArgs(errors.New("some error"), erx.Operation("DB.FetchRecord"))
prebuilt severity
erx.WithArgs(errors.New("some error"), erx.SeverityError)
custom
erx.WithArgs(errors.New("some error"), erx.Severity("Low"))
erx.WithArgs(errors.New("some error"), erx.SeverityError, erx.ValidationError, erx.Operation("Client.Validate"))
erx.WithArgs(erx.Operation("Handler.UpdateUser"), erx.WithArgs(erx.Operation("Service.Update"), errors.New("some error"))
Returns the cause of error of top most error in stack
err := erx.WithArgs(errors.New("some error"))
err.Error() // "some error"
Returns the first non-empty kind in error stack
flat erx
erx.WithArgs(errors.New("some error"), erx.ValidationError)
err.Kind() // erx.ValidationError
nested erx
erx.WithArgs(erx.Operation("Handler.UpdateUser"), erx.WithArgs(erx.ValidationError, errors.New("some error"))
err.Kind() // erx.ValidationError
Returns all the operations in stack
flat erx
erx.WithArgs(errors.New("some error"), erx.Operation("Handler.Update"))
err.Operations() // {"Handler.Update"}
nested erx
erx.WithArgs(erx.Operation("Handler.Update"), erx.WithArgs(erx.Operation("Service.Update"), errors.New("some error"))
err.Operations() // {"Handler.Update", "Service.Update"}
Returns json representation of entire error stack
erx.WithArgs(erx.Operation("Handler.Update"), erx.Severity("Low"), erx.WithArgs(erx.Kind("validationError"), erx.Severity("Med"), errors.New("some error"))
err.String() // {"operation":"Handler.Update","severity":"Low","cause":{"kind":"validationError","severity":"Med","cause":"some error"}}
go get -u github.com/nsnikhil/erx
- Fork it (https://github.com/nsnikhil/erx)
- Create your feature branch (
git checkout -b feature/fooBar
) - Commit your changes (
git commit -am 'Add some fooBar'
) - Push to the branch (
git push origin feature/fooBar
) - Create a new Pull Request
Copyright 2021 Nikhil Soni
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.