github.com/blend/go-sdk@v1.20220411.3/ex/doc.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  /*
     9  Package ex provides the foundations for error handling in the SDK tree.
    10  
    11  To create an error that includes a given string class and stack trace:
    12  
    13  	err := ex.New("this is a structured error")
    14  	...
    15  	fmt.Println(ex.ErrStackTrace(err))
    16  
    17  
    18  When in doubt, wrap any errors from non-sdk methods with an exception:
    19  
    20  	res, err := http.Get(...)
    21  	if err != nil {
    22  		return nil, ex.New(err) // stack trace will originate from this ca..
    23  	}
    24  
    25  To create an error from a known error class, that can be used later to check the type of the error:
    26  
    27  	var ErrTooManyFoos ex.Class = "too many foos"
    28  	...
    29  	err := ex.New(ErrTooManyFoos)
    30  	...
    31  	if ex.Is(err, ErrTooManyFoos) { // we can now verify the type of the err with `ex.Is(err, class)`
    32  		fmt.Println("We did too many foos!")
    33  	}
    34  
    35  We can pass other options to the `ex.New(...)` constructor, such as setting an inner error:
    36  
    37  	err := ex.New(ErrValidation, ex.OptInner(err))
    38  	...
    39  	if ex.Is(err, ErrValidation) {
    40  		fmt.Printf("validation error: %v\n", ex.ErrInner(err))
    41  	}
    42  */
    43  package ex // import "github.com/blend/go-sdk/ex"