github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/common/apperr/error.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2016-2023, Filippov Alex
     4  //
     5  // This library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU Lesser General Public
     7  // License as published by the Free Software Foundation; either
     8  // version 3 of the License, or (at your option) any later version.
     9  //
    10  // This library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  // Library General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public
    16  // License along with this library.  If not, see
    17  // <https://www.gnu.org/licenses/>.
    18  
    19  package apperr
    20  
    21  import (
    22  	"fmt"
    23  
    24  	"github.com/pkg/errors"
    25  
    26  	"github.com/go-playground/validator/v10"
    27  )
    28  
    29  type Error struct {
    30  	code string
    31  	err  error
    32  	root error
    33  	errs validator.ValidationErrorsTranslations
    34  }
    35  
    36  func ErrorWithCode(code, err string, root error) error {
    37  	return &Error{
    38  		code: code,
    39  		err:  errors.New(err),
    40  		root: root,
    41  	}
    42  }
    43  
    44  func (e *Error) Error() string {
    45  	return fmt.Sprintf("%s: %s", e.root.Error(), e.err.Error())
    46  }
    47  
    48  func (e *Error) Code() string {
    49  	return e.code
    50  }
    51  
    52  func (e *Error) Message() string {
    53  	return e.err.Error()
    54  }
    55  
    56  func (e *Error) Root() string {
    57  	return e.root.Error()
    58  }
    59  
    60  func (e *Error) Is(err error) bool {
    61  	if err == nil {
    62  		return false
    63  	}
    64  	return errors.Is(e.root, err)
    65  }
    66  
    67  func (e *Error) SetValidationErrors(errs validator.ValidationErrorsTranslations) {
    68  	e.errs = errs
    69  }
    70  
    71  func (e *Error) ValidationErrors() validator.ValidationErrorsTranslations {
    72  	return e.errs
    73  }
    74  
    75  // Unwrap do not use this method directly. Use errors.Unwrap()
    76  func (e *Error) Unwrap() error {
    77  	return e.root
    78  }
    79  
    80  func Message(err error) string {
    81  	e := GetError(err)
    82  	if e != nil && e.err != nil {
    83  		return e.err.Error()
    84  	}
    85  	return ""
    86  }
    87  
    88  func Code(err error) string {
    89  	e := GetError(err)
    90  	if e != nil {
    91  		return e.code
    92  	}
    93  	return ""
    94  }
    95  
    96  func Root(err error) string {
    97  	e := GetError(err)
    98  	if e != nil && e.root != nil {
    99  		return e.root.Error()
   100  	}
   101  	return ""
   102  }
   103  
   104  func SetValidationErrors(err error, errs validator.ValidationErrorsTranslations) {
   105  	e := GetError(err)
   106  	if e == nil {
   107  		return
   108  	}
   109  	e.errs = errs
   110  }
   111  
   112  func GetError(err error) *Error {
   113  	if err == nil {
   114  		return nil
   115  	}
   116  	if e, ok := err.(*Error); ok {
   117  		return e
   118  	}
   119  	return nil
   120  }