github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/framework/berror/predefine_code.go (about)

     1  // the package is exported from github.com/beego/beego/v2/core/berror
     2  
     3  // Copyright 2023. All Rights Reserved.
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //      http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package berror
    18  
    19  import (
    20  	"fmt"
    21  )
    22  
    23  // pre define code
    24  
    25  // Unknown indicates got some error which is not defined
    26  var Unknown = DefineCode(5000001, "error", "Unknown", fmt.Sprintf(`
    27  Unknown error code. Usually you will see this code in three cases:
    28  1. You forget to define Code or function DefineCode not being executed;
    29  2. This is not Beego's error but you call FromError();
    30  3. Beego got unexpected error and don't know how to handle it, and then return Unknown error
    31  
    32  A common practice to DefineCode looks like:
    33  %s
    34  
    35  In this way, you may forget to import this package, and got Unknown error. 
    36  
    37  Sometimes, you believe you got Beego error, but actually you don't, and then you call FromError(err)
    38  
    39  `, goCodeBlock(`
    40  import your_package
    41  
    42  func init() {
    43      DefineCode(5100100, "your_module", "detail")
    44      // ...
    45  }
    46  `)))
    47  
    48  func goCodeBlock(code string) string {
    49  	return codeBlock("go", code)
    50  }
    51  
    52  func codeBlock(lan string, code string) string {
    53  	return fmt.Sprintf("```%s\n%s\n```", lan, code)
    54  }