gitee.com/hongliu9527/go-tools@v0.0.8/errors/gerror/gerror_error_format.go (about)

     1  /*
     2   * @Author: hongliu
     3   * @Date: 2022-12-29 10:51:12
     4   * @LastEditors: hongliu
     5   * @LastEditTime: 2022-12-29 11:29:11
     6   * @FilePath: \go-tools\errors\gerror\gerror_error_format.go
     7   * @Description:错误格式处理
     8   *
     9   * Copyright (c) 2022 by 洪流, All Rights Reserved.
    10   */
    11  
    12  package gerror
    13  
    14  import (
    15  	"fmt"
    16  	"io"
    17  )
    18  
    19  // Format formats the frame according to the fmt.Formatter interface.
    20  //
    21  // %v, %s   : Print all the error string;
    22  // %-v, %-s : Print current level error string;
    23  // %+s      : Print full stack error list;
    24  // %+v      : Print the error string and full stack error list
    25  func (err *Error) Format(s fmt.State, verb rune) {
    26  	switch verb {
    27  	case 's', 'v':
    28  		switch {
    29  		case s.Flag('-'):
    30  			if err.text != "" {
    31  				_, _ = io.WriteString(s, err.text)
    32  			} else {
    33  				_, _ = io.WriteString(s, err.Error())
    34  			}
    35  		case s.Flag('+'):
    36  			if verb == 's' {
    37  				_, _ = io.WriteString(s, err.Stack())
    38  			} else {
    39  				_, _ = io.WriteString(s, err.Error()+"\n"+err.Stack())
    40  			}
    41  		default:
    42  			_, _ = io.WriteString(s, err.Error())
    43  		}
    44  	}
    45  }