...

Source file src/gitlab.com/tymonx/go-logger/logger/runtime_error.go

Documentation: gitlab.com/tymonx/go-logger/logger

     1  // Copyright 2020 Tymoteusz Blazejczyk
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package logger
    16  
    17  import (
    18  	"fmt"
    19  	"path/filepath"
    20  	"runtime"
    21  )
    22  
    23  // These constants are used for the RuntimeError.
    24  const (
    25  	RuntimeErrorSkipCall = 1
    26  )
    27  
    28  // RuntimeError defines runtime error with returned error message, file name,
    29  // file line number and function name.
    30  type RuntimeError struct {
    31  	line      int
    32  	file      string
    33  	message   string
    34  	function  string
    35  	arguments []interface{}
    36  }
    37  
    38  // NewRuntimeError creates new RuntimeError object.
    39  func NewRuntimeError(message string, arguments ...interface{}) *RuntimeError {
    40  	return NewRuntimeErrorBase(RuntimeErrorSkipCall, message, arguments...)
    41  }
    42  
    43  // NewRuntimeErrorBase creates new RuntimeError object using custom skip call value.
    44  func NewRuntimeErrorBase(skipCall int, message string, arguments ...interface{}) *RuntimeError {
    45  	pc, path, line, _ := runtime.Caller(skipCall + 1)
    46  
    47  	return &RuntimeError{
    48  		line:      line,
    49  		file:      filepath.Base(path),
    50  		message:   message,
    51  		function:  filepath.Base(runtime.FuncForPC(pc).Name()),
    52  		arguments: arguments,
    53  	}
    54  }
    55  
    56  // Error returns formatted error string with message, file name, file line
    57  // number and function name.
    58  func (r *RuntimeError) Error() string {
    59  	var formatted string
    60  
    61  	var err error
    62  
    63  	record := &Record{
    64  		Message:   r.message,
    65  		Arguments: r.arguments,
    66  	}
    67  
    68  	if formatted, err = NewFormatter().FormatMessage(record); err != nil {
    69  		formatted = r.message
    70  	}
    71  
    72  	return fmt.Sprintf("%s:%d:%s(): %s",
    73  		r.file,
    74  		r.line,
    75  		r.function,
    76  		formatted,
    77  	)
    78  }
    79  
    80  // Unwrap wrapped error.
    81  func (r *RuntimeError) Unwrap() error {
    82  	for _, argument := range r.arguments {
    83  		if err, ok := argument.(error); ok {
    84  			return err
    85  		}
    86  	}
    87  
    88  	return nil
    89  }
    90  

View as plain text