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

     1  /*
     2  
     3  Copyright (c) 2024 - 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  When in doubt, wrap any errors from non-sdk methods with an exception:
    18  
    19  	res, err := http.Get(...)
    20  	if err != nil {
    21  		return nil, ex.New(err) // stack trace will originate from this call
    22  	}
    23  
    24  To create an error from a known error class, that can be used later to check the type of the error:
    25  
    26  	var ErrTooManyFoos ex.Class = "too many foos"
    27  	...
    28  	err := ex.New(ErrTooManyFoos)
    29  	...
    30  	if ex.Is(err, ErrTooManyFoos) { // we can now verify the type of the err with `ex.Is(err, class)`
    31  		fmt.Println("We did too many foos!")
    32  	}
    33  
    34  We can pass other options to the `ex.New(...)` constructor, such as setting an inner error:
    35  
    36  	err := ex.New(ErrValidation, ex.OptInner(err))
    37  	...
    38  	if ex.Is(err, ErrValidation) {
    39  		fmt.Printf("validation error: %v\n", ex.ErrInner(err))
    40  	}
    41  */
    42  package ex // import "github.com/blend/go-sdk/ex"