mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-13 19:56:38 +05:00
APIErrorHandler
This commit is contained in:
@@ -17,41 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func NewErrMandatoryIeMissing(fields ...string) error {
|
||||
return fmt.Errorf("MANDATORY_IE_MISSING:%v", fields)
|
||||
}
|
||||
|
||||
func NewErrServerError(err error) error {
|
||||
return fmt.Errorf("SERVER_ERROR: %s", err)
|
||||
}
|
||||
|
||||
var (
|
||||
ErrNotImplemented = errors.New("NOT_IMPLEMENTED")
|
||||
ErrNotFound = errors.New("NOT_FOUND")
|
||||
ErrTimedOut = errors.New("TIMED_OUT")
|
||||
ErrServerError = errors.New("SERVER_ERROR")
|
||||
ErrMaxRecursionDepth = errors.New("MAX_RECURSION_DEPTH")
|
||||
ErrMandatoryIeMissing = errors.New("MANDATORY_IE_MISSING")
|
||||
ErrExists = errors.New("EXISTS")
|
||||
ErrBrokenReference = errors.New("BROKEN_REFERENCE")
|
||||
ErrParserError = errors.New("PARSER_ERROR")
|
||||
ErrInvalidPath = errors.New("INVALID_PATH")
|
||||
ErrInvalidKey = errors.New("INVALID_KEY")
|
||||
ErrUnauthorizedDestination = errors.New("UNAUTHORIZED_DESTINATION")
|
||||
ErrRatingPlanNotFound = errors.New("RATING_PLAN_NOT_FOUND")
|
||||
ErrAccountNotFound = errors.New("ACCOUNT_NOT_FOUND")
|
||||
ErrAccountDisabled = errors.New("ACCOUNT_DISABLED")
|
||||
ErrUserNotFound = errors.New("USER_NOT_FOUND")
|
||||
ErrInsufficientCredit = errors.New("INSUFFICIENT_CREDIT")
|
||||
ErrNotConvertible = errors.New("NOT_CONVERTIBLE")
|
||||
ErrResourceUnavailable = errors.New("RESOURCE_UNAVAILABLE")
|
||||
ErrNoActiveSession = errors.New("NO_ACTIVE_SESSION")
|
||||
|
||||
CdreCdrFormats = []string{CSV, DRYRUN, CDRE_FIXED_WIDTH}
|
||||
PrimaryCdrFields = []string{CGRID, CDRSOURCE, CDRHOST, ACCID, TOR, REQTYPE, DIRECTION, TENANT, CATEGORY, ACCOUNT, SUBJECT, DESTINATION, SETUP_TIME, PDD, ANSWER_TIME, USAGE,
|
||||
SUPPLIER, DISCONNECT_CAUSE, COST, RATED, PartialField, MEDI_RUNID}
|
||||
|
||||
@@ -17,6 +17,34 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotImplemented = errors.New("NOT_IMPLEMENTED")
|
||||
ErrNotFound = errors.New("NOT_FOUND")
|
||||
ErrTimedOut = errors.New("TIMED_OUT")
|
||||
ErrServerError = errors.New("SERVER_ERROR")
|
||||
ErrMaxRecursionDepth = errors.New("MAX_RECURSION_DEPTH")
|
||||
ErrMandatoryIeMissing = errors.New("MANDATORY_IE_MISSING")
|
||||
ErrExists = errors.New("EXISTS")
|
||||
ErrBrokenReference = errors.New("BROKEN_REFERENCE")
|
||||
ErrParserError = errors.New("PARSER_ERROR")
|
||||
ErrInvalidPath = errors.New("INVALID_PATH")
|
||||
ErrInvalidKey = errors.New("INVALID_KEY")
|
||||
ErrUnauthorizedDestination = errors.New("UNAUTHORIZED_DESTINATION")
|
||||
ErrRatingPlanNotFound = errors.New("RATING_PLAN_NOT_FOUND")
|
||||
ErrAccountNotFound = errors.New("ACCOUNT_NOT_FOUND")
|
||||
ErrAccountDisabled = errors.New("ACCOUNT_DISABLED")
|
||||
ErrUserNotFound = errors.New("USER_NOT_FOUND")
|
||||
ErrInsufficientCredit = errors.New("INSUFFICIENT_CREDIT")
|
||||
ErrNotConvertible = errors.New("NOT_CONVERTIBLE")
|
||||
ErrResourceUnavailable = errors.New("RESOURCE_UNAVAILABLE")
|
||||
ErrNoActiveSession = errors.New("NO_ACTIVE_SESSION")
|
||||
)
|
||||
|
||||
// NewCGRError initialises a new CGRError
|
||||
func NewCGRError(context, apiErr, shortErr, longErr string) *CGRError {
|
||||
return &CGRError{context: context, apiError: apiErr,
|
||||
@@ -52,3 +80,25 @@ func (err *CGRError) ActivateShortError() {
|
||||
func (err *CGRError) ActivateLongError() {
|
||||
err.errorMessage = err.longError
|
||||
}
|
||||
|
||||
func NewErrMandatoryIeMissing(fields ...string) error {
|
||||
return fmt.Errorf("MANDATORY_IE_MISSING:%v", fields)
|
||||
}
|
||||
|
||||
func NewErrServerError(err error) error {
|
||||
return fmt.Errorf("SERVER_ERROR: %s", err)
|
||||
}
|
||||
|
||||
// Centralized returns for APIs
|
||||
func APIErrorHandler(err error) error {
|
||||
cgrErr, ok := err.(*CGRError)
|
||||
if !ok {
|
||||
if err == ErrNotFound {
|
||||
return err
|
||||
} else {
|
||||
return NewErrServerError(err)
|
||||
}
|
||||
}
|
||||
cgrErr.ActivateAPIError()
|
||||
return cgrErr
|
||||
}
|
||||
|
||||
@@ -27,6 +27,9 @@ func TestCGRErrorActivate(t *testing.T) {
|
||||
shortErr := "short error"
|
||||
longErr := "long error which is good for debug"
|
||||
err := NewCGRError(ctx, apiErr, shortErr, longErr)
|
||||
if ctxRcv := err.Context(); ctxRcv != ctx {
|
||||
t.Errorf("Context: <%s>", ctxRcv)
|
||||
}
|
||||
if err.Error() != shortErr {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -39,3 +42,16 @@ func TestCGRErrorActivate(t *testing.T) {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAPIErrorHandler(t *testing.T) {
|
||||
if err := APIErrorHandler(ErrNotImplemented); err.Error() != NewErrServerError(ErrNotImplemented).Error() {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := APIErrorHandler(ErrNotFound); err.Error() != ErrNotFound.Error() {
|
||||
t.Error(err)
|
||||
}
|
||||
cgrErr := NewCGRError("TEST_CONTEXT", "TEST_API_ERR", "short error", "long error which is good for debug")
|
||||
if err := APIErrorHandler(cgrErr); err.Error() != cgrErr.apiError {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user