Exemplo n.º 1
0
// Copyright 2010-2016 Joubin Houshyar.  All rights reserved.
// Use of this source code is governed by a 2-clause BSD
// license that can be found in the LICENSE file.

// whitebox tests
package goerror_test

import (
	"errors"
	"goerror"
	"testing"
)

var fubarError = goerror.Define("fubar")

func TestDefine(t *testing.T) {
	// both test applicability of TypeOf() for any error
	// and test that match fails
	e0 := errors.New("generic")
	if goerror.TypeOf(e0).Is(fubarError) {
		t.Errorf("e0 is not an fubarError (error)")
	}

	// match the len of the error category string
	// to insure we're not simply matching string lengths
	errPrefix := "error - " // REVU: don't like this in general but in a whitebox test?
	e1 := errors.New(errPrefix + "spoO")
	if goerror.TypeOf(e1).Is(fubarError) {
		t.Errorf("e1 is not an fubarError (error)")
	}
Exemplo n.º 2
0
// Copyright 2010-2016 Joubin Houshyar.  All rights reserved.
// Use of this source code is governed by a 2-clause BSD
// license that can be found in the LICENSE file.

package goerror_test

import (
	"fmt"
	"goerror"
)

// Let's define a few canonical goerror
var (
	IllegalArgument = goerror.Define("IllegalArgument")
	IllegalState    = goerror.Define("IllegalState")
	AccessDenied    = goerror.Define("AccessDenied")
	Bug             = goerror.Define("BUG")
)

// Example defining, returning, and checking goerror
func ExampleError() {

	user := "******"
	oldpw := "old-secret"
	newpw := "new-secret"

	if e := ChangePassword(user, oldpw, newpw); e != nil {
		switch typ := goerror.TypeOf(e); {
		case typ.Is(IllegalArgument): /* handle it */
		case typ.Is(IllegalState): /* handle it */
		case typ.Is(AccessDenied): /* handle it */
Exemplo n.º 3
0
// Copyright 2011-2016 Joubin Houshyar.  All rights reserved.
// Use of this source code is governed by a 2-clause BSD
// license that can be found in the LICENSE file.

// Package contextual defines the semantics of a generalized
// hierarchical namespace of string names, and untyped values
// that serve as the operational context for components.
package contextual

import (
	"goerror"
)

var (
	/* - general errors - */
	IllegalArgumentError = goerror.Define("illegal argument")
	IllegalStateError    = goerror.Define("illegal state")
	NilParentError       = goerror.Define("parent is nil")
	NilNameError         = goerror.Define("name is nil/zero-value")
	NegativeNArgError    = goerror.Define("hierchy walk steps 'n' is negative")

	/* - binding op errors - */
	NilValueError      = goerror.Define("nil values are not allowed")
	AlreadyBoundError  = goerror.Define("already bound error")
	NoSuchBindingError = goerror.Define("no such binding")
)

// ----------------------------------------------------------------------------
// Contextual API
// ----------------------------------------------------------------------------