github.com/wangyougui/gf/v2@v2.6.5/errors/gerror/gerror_error_format.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package gerror
     8  
     9  import (
    10  	"fmt"
    11  	"io"
    12  )
    13  
    14  // Format formats the frame according to the fmt.Formatter interface.
    15  //
    16  // %v, %s   : Print all the error string;
    17  // %-v, %-s : Print current level error string;
    18  // %+s      : Print full stack error list;
    19  // %+v      : Print the error string and full stack error list
    20  func (err *Error) Format(s fmt.State, verb rune) {
    21  	switch verb {
    22  	case 's', 'v':
    23  		switch {
    24  		case s.Flag('-'):
    25  			if err.text != "" {
    26  				_, _ = io.WriteString(s, err.text)
    27  			} else {
    28  				_, _ = io.WriteString(s, err.Error())
    29  			}
    30  		case s.Flag('+'):
    31  			if verb == 's' {
    32  				_, _ = io.WriteString(s, err.Stack())
    33  			} else {
    34  				_, _ = io.WriteString(s, err.Error()+"\n"+err.Stack())
    35  			}
    36  		default:
    37  			_, _ = io.WriteString(s, err.Error())
    38  		}
    39  	}
    40  }