vitess.io/vitess@v0.16.2/go/vt/vterrors/vterrorsgen/main.go (about) 1 /* 2 Copyright 2022 The Vitess 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 http://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 main 18 19 import ( 20 "log" 21 "os" 22 "strings" 23 "text/template" 24 25 "vitess.io/vitess/go/mysql" 26 27 "vitess.io/vitess/go/vt/vterrors" 28 ) 29 30 const ( 31 tmpl = ` 32 | ID | Description | Error | MySQL Error Code | SQL State | 33 | --- | --- | --- | --- | --- | 34 {{- range $err := . }} 35 {{- $data := (call $err) }} 36 | {{ $data.ID }} | {{ $data.Description }} | {{ FormatError $data.Err }} | {{ ConvertStateToMySQLErrorCode $data.State }} | {{ ConvertStateToMySQLState $data.State }} | 37 {{- end }} 38 ` 39 ) 40 41 // This program reads the errors located in the `vitess.io/vitess/go/vt/vterrors` package 42 // and prints on the standard output a table, in Markdown format, that lists all the 43 // errors with their code, description, error content, mysql error code and the SQL state. 44 func main() { 45 t := template.New("template") 46 t.Funcs(map[string]any{ 47 "ConvertStateToMySQLErrorCode": mysql.ConvertStateToMySQLErrorCode, 48 "ConvertStateToMySQLState": mysql.ConvertStateToMySQLState, 49 "FormatError": func(err error) string { 50 s := err.Error() 51 return strings.TrimSpace(strings.Join(strings.Split(s, ":")[1:], ":")) 52 }, 53 }) 54 t = template.Must(t.Parse(tmpl)) 55 56 err := t.ExecuteTemplate(os.Stdout, "template", vterrors.Errors) 57 if err != nil { 58 log.Fatal(err) 59 } 60 }