github.com/go-spring/spring-base@v1.1.3/util/error.go (about)

     1  /*
     2   * Copyright 2012-2019 the original author or authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      https://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package util
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  )
    23  
    24  // ForbiddenMethod throws this error when calling a method is prohibited.
    25  var ForbiddenMethod = errors.New("forbidden method")
    26  
    27  // UnimplementedMethod throws this error when calling an unimplemented method.
    28  var UnimplementedMethod = errors.New("unimplemented method")
    29  
    30  var WrapFormat = func(err error, fileline string, format string, a ...interface{}) error {
    31  	if err == nil {
    32  		if format != "" {
    33  			return fmt.Errorf(fileline+" "+format, a...)
    34  		}
    35  		return errors.New(fileline + " " + fmt.Sprint(a...))
    36  	}
    37  	if format == "" {
    38  		return fmt.Errorf("%s %s; %w", fileline, fmt.Sprint(a...), err)
    39  	}
    40  	return fmt.Errorf("%s %s; %w", fileline, fmt.Sprintf(format, a...), err)
    41  }
    42  
    43  // Error returns an error with the file and line.
    44  // The file and line may be calculated at the compile time in the future.
    45  func Error(fileline string, text string) error {
    46  	return WrapFormat(nil, fileline, "", text)
    47  }
    48  
    49  // Errorf returns an error with the file and line.
    50  // The file and line may be calculated at the compile time in the future.
    51  func Errorf(fileline string, format string, a ...interface{}) error {
    52  	return WrapFormat(nil, fileline, format, a...)
    53  }
    54  
    55  // Wrap returns an error with the file and line.
    56  // The file and line may be calculated at the compile time in the future.
    57  func Wrap(err error, fileline string, text string) error {
    58  	return WrapFormat(err, fileline, "", text)
    59  }
    60  
    61  // Wrapf returns an error with the file and line.
    62  // The file and line may be calculated at the compile time in the future.
    63  func Wrapf(err error, fileline string, format string, a ...interface{}) error {
    64  	return WrapFormat(err, fileline, format, a...)
    65  }