github.com/blend/go-sdk@v1.20220411.3/web/errors.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 package web 9 10 import ( 11 "github.com/golang-jwt/jwt" 12 13 "github.com/blend/go-sdk/ex" 14 ) 15 16 const ( 17 // ErrSessionIDEmpty is thrown if a session id is empty. 18 ErrSessionIDEmpty ex.Class = "auth session id is empty" 19 // ErrSecureSessionIDEmpty is an error that is thrown if a given secure session id is invalid. 20 ErrSecureSessionIDEmpty ex.Class = "auth secure session id is empty" 21 // ErrUnsetViewTemplate is an error that is thrown if a given secure session id is invalid. 22 ErrUnsetViewTemplate ex.Class = "view result template is unset" 23 // ErrParameterMissing is an error on request validation. 24 ErrParameterMissing ex.Class = "parameter is missing" 25 // ErrParameterInvalid is an error on request validation. 26 ErrParameterInvalid ex.Class = "parameter is invalid" 27 ) 28 29 // NewParameterMissingError returns a new parameter missing error. 30 func NewParameterMissingError(paramName string) error { 31 return ex.New(ErrParameterMissing, ex.OptMessagef("%s", paramName)) 32 } 33 34 // NewParameterInvalidError returns a new parameter invalid error. 35 func NewParameterInvalidError(paramName, message string) error { 36 return ex.New(ErrParameterMissing, ex.OptMessagef("%q: %s", paramName, message)) 37 } 38 39 // IsErrSessionInvalid returns if an error is a session invalid error. 40 func IsErrSessionInvalid(err error) bool { 41 if err == nil { 42 return false 43 } 44 if ex.Is(err, ErrSessionIDEmpty) || 45 ex.Is(err, ErrSecureSessionIDEmpty) || 46 isValidationError(err) { 47 return true 48 } 49 return false 50 } 51 52 func isValidationError(err error) bool { 53 _, ok := err.(*jwt.ValidationError) 54 return ok 55 } 56 57 // IsErrBadRequest returns if an error is a bad request triggering error. 58 func IsErrBadRequest(err error) bool { 59 return IsErrParameterMissing(err) || IsErrParameterInvalid(err) 60 } 61 62 // IsErrParameterMissing returns if an error is an ErrParameterMissing. 63 func IsErrParameterMissing(err error) bool { 64 if err == nil { 65 return false 66 } 67 return ex.Is(err, ErrParameterMissing) 68 } 69 70 // IsErrParameterInvalid returns if an error is an ErrParameterInvalid. 71 func IsErrParameterInvalid(err error) bool { 72 if err == nil { 73 return false 74 } 75 return ex.Is(err, ErrParameterMissing) 76 }