Skip to content

Commit

Permalink
Initial version - NilOrPanicX.
Browse files Browse the repository at this point in the history
  • Loading branch information
pat42smith committed Aug 24, 2022
0 parents commit 7cd3f2e
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2022 Patrick Smith

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.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/pat42smith/goerrors

go 1.18

require github.com/pat42smith/gotest v0.0.1 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/pat42smith/gotest v0.0.1 h1:NaeFsxxbfcZiFkLy8ywx89W9RWKldYW71mln4TOzCzw=
github.com/pat42smith/gotest v0.0.1/go.mod h1:0q1IE4xoC2Nm07gIOXefKF73yrAWBjq90d6RS67dEF8=
28 changes: 28 additions & 0 deletions goerrors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2022 Patrick Smith
// Use of this source code is subject to the MIT-style license in the LICENSE file.

// Package goerrors provides a few simple functions for handling errors in Go.
package goerrors

// NilOrPanic panics with e if e is not nil.
func NilOrPanic(e error) {
if e != nil {
panic(e)
}
}

// NilOrPanic1 panics with e if e is not nil. Otherwise, it returns t.
func NilOrPanic1[T any](t T, e error) T {
if e != nil {
panic(e)
}
return t
}

// NilOrPanic2 panics with e if e is not nil. Otherwise, it returns (t1, t2).
func NilOrPanic2[T1, T2 any](t1 T1, t2 T2, e error) (T1, T2) {
if e != nil {
panic(e)
}
return t1, t2
}
55 changes: 55 additions & 0 deletions goerrors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2022 Patrick Smith
// Use of this source code is subject to the MIT-style license in the LICENSE file.

package goerrors

import (
"fmt"
"testing"

"github.com/pat42smith/gotest"
)

func TestNilOrPanic(t *testing.T) {
NilOrPanic(nil)

e := gotest.MustPanic(t, func() {
NilOrPanic(fmt.Errorf("oops!"))
})
gotest.Expect(t, "oops!", e.(error).Error())
}

func TestNilOrPanic1(t *testing.T) {
var x byte = 99
gotest.Expect(t, x, NilOrPanic1(x, nil))

e := gotest.MustPanic(t, func() {
NilOrPanic1(x, fmt.Errorf("broken"))
})
gotest.Expect(t, "broken", e.(error).Error())

foo := func() (byte, error) {
return x, nil
}
gotest.Expect(t, x, NilOrPanic1(foo()))
}

func TestNilOrPanic2(t *testing.T) {
x := "seventeen"
y := 3.7
u, v := NilOrPanic2(x, y, nil)
gotest.Expect(t, x, u)
gotest.Expect(t, y, v)

e := gotest.MustPanic(t, func() {
NilOrPanic2(x, y, fmt.Errorf("sabotaged"))
})
gotest.Expect(t, "sabotaged", e.(error).Error())

foo := func() (string, float64, error) {
return x, y, nil
}
u, v = NilOrPanic2(foo())
gotest.Expect(t, x, u)
gotest.Expect(t, y, v)
}

0 comments on commit 7cd3f2e

Please sign in to comment.