Skip to content

Commit

Permalink
add package errext
Browse files Browse the repository at this point in the history
The ErrorSet type comes from Limes. I want to use it in Castellum, too.
  • Loading branch information
majewsky committed Apr 13, 2023
1 parent cee52bb commit 8e821f1
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ reusability. Feel free to add to this.
* [assert](./assert) contains various assertions for unit tests.
* [audittools](./audittools) contains helper functions for establishing a connection to a RabbitMQ server (with sane defaults) and publishing messages to it.
* [easypg](./easypg) is a database library for applications that use PostgreSQL. It integrates [golang-migrate/migrate](https://github.com/golang-migrate/migrate) for data definition and imports the libpq-based SQL driver.
* [errext](./errext) contains convenience functions for handling and propagating errors.
* [gopherpolicy](./gopherpolicy) integrates [Gophercloud](https://github.com/gophercloud/gophercloud) with [goslo.policy](https://github.com/databus23/goslo.policy), for OpenStack services that need to validate client tokens and check permissions.
* [httpapi](./httpapi) contains opinionated base machinery for assembling and exposing an API consisting of HTTP endpoints.
* [httpext](./httpext) adds some convenience functions to [net/http](https://golang.org/pkg/http/).
Expand Down
21 changes: 21 additions & 0 deletions errext/errext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*******************************************************************************
*
* Copyright 2023 SAP SE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You should have received a copy of the License along with this
* program. If not, 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.
*
*******************************************************************************/

// Package errext contains convenience functions for handling and propagating errors.
package errext
67 changes: 67 additions & 0 deletions errext/errorset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*******************************************************************************
*
* Copyright 2023 SAP SE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You should have received a copy of the License along with this
* program. If not, 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.
*
*******************************************************************************/

package errext

import (
"fmt"
"os"

"github.com/sapcc/go-bits/logg"
)

// ErrorSet replaces the "error" return value in functions that can return
// multiple errors. It provides convenience functions for easily adding errors
// to the set.
type ErrorSet []error

// Add adds the given error to the set if it is non-nil.
func (errs *ErrorSet) Add(err error) {
if err != nil {
*errs = append(*errs, err)
}
}

// Addf is a shorthand for errs.Add(fmt.Errorf(...)).
func (errs *ErrorSet) Addf(msg string, args ...any) {
*errs = append(*errs, fmt.Errorf(msg, args...))
}

// Append adds all errors from the `other` ErrorSet to this one.
func (errs *ErrorSet) Append(other ErrorSet) {
*errs = append(*errs, other...)
}

// IsEmpty returns true if no errors are in the set.
func (errs ErrorSet) IsEmpty() bool {
return len(errs) == 0
}

// LogFatalIfError reports all errors in this set on level FATAL, thus dying if
// there are any errors.
func (errs ErrorSet) LogFatalIfError() {
hasErrors := false
for _, err := range errs {
hasErrors = true
logg.Other("FATAL", err.Error())
}
if hasErrors {
os.Exit(1)
}
}

0 comments on commit 8e821f1

Please sign in to comment.