...

Source file src/edge-infra.dev/pkg/edge/api/apierror/error.go

Documentation: edge-infra.dev/pkg/edge/api/apierror

     1  package apierror
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  
     7  	"edge-infra.dev/pkg/lib/fog"
     8  
     9  	"edge-infra.dev/pkg/lib/runtime/version"
    10  
    11  	"github.com/99designs/gqlgen/graphql"
    12  	"github.com/vektah/gqlparser/v2/ast"
    13  )
    14  
    15  const (
    16  	// OperationIDKey key that represents the operationID of the current request.
    17  	OperationIDKey = "operationID"
    18  	// VersionKey key that represents the BFF version that is executing the request.
    19  	VersionKey = "version"
    20  	// AdditionalKey key that represents the extra details that are included in the error response.
    21  	AdditionalKey = "additional"
    22  	// BannerCheckKey key that represents the details of the bsl and sql banner check
    23  	BannerCheckKey = "bannerCheckDetails"
    24  	// SiteCheckKey key that represents the details of the bsl and bsl-info config site check
    25  	SiteCheckKey = "siteCheckDetails"
    26  	// DeleteClustersKey key that represents the error of delete the single cluster
    27  	DeleteClustersKey = "deleteClusterDetails"
    28  	// BannerCheckMessage is the error message of banners check errors
    29  	BannerCheckMessage = "Mismatches in bsl and sql banners"
    30  	// SiteCheckMessage is the error message of site check errors
    31  	SiteCheckMessage = "Mismatches in bsl and bsl-info config map site"
    32  	// DeleteClustersMessage is the error message of delete clusters errors
    33  	DeleteClustersMessage = "DeleteClustersErrors"
    34  	// HelmChartError is an error interacting with helm repos
    35  	HelmChartError = "Error getting helm chart information"
    36  	// DetailedHelmError key that represents the details of the bsl and bsl-info config site check
    37  	DetailedHelmError = "helmApiErrors"
    38  	// Create ChannelIAMPolicy binding Error
    39  	CreateChannelIAMPolicyError        = "CreateChannelIAMPolicyErrors"
    40  	CreateChannelIAMPolicyErrorMessage = "there was an error creating channel iam policy member for: %s"
    41  
    42  	// Getting list of Channels Error
    43  	GetChannelsAPIError    = "GetChannelsAPIError"
    44  	GetChannelsAPIErrorMsg = "there was an error getting list of channels"
    45  	GetChannelAPIErrorMsg  = "there was an error getting channel: %s"
    46  
    47  	// Create Channel Error
    48  	CreateChannelAPIError    = "CreateChannelAPIError"
    49  	CreateChannelAPIErrorMsg = "there was an error creating channel: %s"
    50  
    51  	// Rotate Channel Error
    52  	ChannelRotateError    = "ChannelRotateError"
    53  	ChannelRotateErrorMsg = "failed to fetch new rotated channel: %s"
    54  )
    55  
    56  // Error represents a BFF error response.
    57  type Error struct {
    58  	// Err the original error.
    59  	Err error `json:"-"`
    60  	// path is the graphql path.
    61  	path ast.Path `json:"-"`
    62  	// Message is the short readable text returned to the client/UI.
    63  	Message string `json:"message"`
    64  	// OperationID is the reference id that is logged when an error occurs in BFF
    65  	// this id can be used to trace the error stacktrace.
    66  	OperationID string `json:"operationID"`
    67  	// Version is the BFF Version that returned the error response.
    68  	Version string `json:"version"`
    69  	// Extensions
    70  	Ext map[string]interface{} `json:"Extensions"`
    71  }
    72  
    73  // Wrap converts the original error to toAPIError
    74  func Wrap(err error) *Error {
    75  	if err == nil {
    76  		return nil
    77  	}
    78  	return &Error{
    79  		Err:     err,
    80  		Message: err.Error(),
    81  		Version: version.New().SemVer,
    82  		Ext:     make(map[string]interface{}),
    83  	}
    84  }
    85  
    86  // New creates a new error and sets the message.
    87  // can be used like so:
    88  // err := errors.New("internal server error") //where errors is the standard library error pkg
    89  // New(err.Error())
    90  func New(msg string) *Error {
    91  	return Wrap(errors.New(msg))
    92  }
    93  
    94  // Error implements the error interface.
    95  func (g *Error) Error() string {
    96  	return g.Message
    97  }
    98  
    99  // Unwrap retrieves the original error.
   100  func (g *Error) Unwrap() error {
   101  	if g == nil {
   102  		return nil
   103  	}
   104  	return g.Err
   105  }
   106  
   107  // SetPath sets the graphql ast path for the BFF generic error.
   108  func (g *Error) SetPath(ctx context.Context) *Error {
   109  	g.path = graphql.GetPath(ctx)
   110  	return g
   111  }
   112  
   113  // SetMessage sets the message for the error response.
   114  func (g *Error) SetMessage(msg string) *Error {
   115  	g.Message = msg
   116  	return g
   117  }
   118  
   119  // SetOperationID sets the operation id for the bff error response.
   120  func (g *Error) SetOperationID(ctx context.Context) *Error {
   121  	g.OperationID = fog.OperationID(ctx)
   122  	return g
   123  }
   124  
   125  // SetVersion sets the Version for the bff error response.
   126  func (g *Error) SetVersion(Version string) *Error {
   127  	g.Version = Version
   128  	return g
   129  }
   130  
   131  // SetGenericErrorExtension sets the generic error extension.
   132  func (g *Error) SetGenericErrorExtension(exts map[string]interface{}) *Error {
   133  	g.Ext = exts
   134  	return g
   135  }
   136  
   137  // AddGenericErrorExtension adds a new generic error extension.
   138  func (g *Error) AddGenericErrorExtension(key string, value interface{}) *Error {
   139  	if g.Ext == nil {
   140  		g.Ext = map[string]interface{}{}
   141  	}
   142  	g.Ext[key] = value
   143  	return g
   144  }
   145  
   146  // GetGenericErrorExtension gets the generic error extension.
   147  func (g *Error) GetGenericErrorExtension(key string) interface{} {
   148  	return g.Ext[key]
   149  }
   150  
   151  // Extensions maps the bff generic error extension to a map that can be used in the extensions field.
   152  func (g *Error) Extensions() map[string]interface{} {
   153  	additional := make(map[string]interface{})
   154  	for key, value := range g.Ext {
   155  		if key != AdditionalKey && key != VersionKey {
   156  			additional[key] = value
   157  		}
   158  	}
   159  	return additional
   160  }
   161  

View as plain text