| 1 |
//go:build ignore |
| 2 |
|
| 3 |
package main |
| 4 |
|
| 5 |
import ( |
| 6 |
"encoding/json" |
| 7 |
"fmt" |
| 8 |
"log" |
| 9 |
"os" |
| 10 |
"os/exec" |
| 11 |
"regexp" |
| 12 |
"sort" |
| 13 |
"strings" |
| 14 |
"text/template" |
| 15 |
|
| 16 |
"github.com/nats-io/nats-server/v2/server" |
| 17 |
) |
| 18 |
|
| 19 |
var tagRe = regexp.MustCompile("\\{(.+?)}") |
| 20 |
|
| 21 |
var templ = ` |
| 22 |
// Generated code, do not edit. See errors.json and run go generate to update |
| 23 |
|
| 24 |
package server |
| 25 |
|
| 26 |
import "strings" |
| 27 |
|
| 28 |
const ( |
| 29 |
{{- range $i, $error := . }} |
| 30 |
{{- if .Comment }} |
| 31 |
// {{ .Constant }} {{ .Comment }} ({{ .Description | print }}) |
| 32 |
{{- else }} |
| 33 |
// {{ .Constant }} {{ .Description | print }} |
| 34 |
{{- end }} |
| 35 |
{{ .Constant }} ErrorIdentifier = {{ .ErrCode }} |
| 36 |
{{ end }} |
| 37 |
) |
| 38 |
|
| 39 |
var ( |
| 40 |
ApiErrors = map[ErrorIdentifier]*ApiError{ |
| 41 |
{{- range $i, $error := . }} |
| 42 |
{{ .Constant }}: {Code: {{ .Code }},ErrCode: {{ .ErrCode }},Description: {{ .Description | printf "%q" }}},{{- end }} |
| 43 |
} |
| 44 |
|
| 45 |
{{- range $i, $error := . }} |
| 46 |
{{- if .Deprecates }} |
| 47 |
// {{ .Deprecates }} Deprecated by {{ .Constant }} ApiError, use IsNatsError() for comparisons |
| 48 |
{{ .Deprecates }} = ApiErrors[{{ .Constant }}] |
| 49 |
{{- end }} |
| 50 |
{{- end }} |
| 51 |
) |
| 52 |
|
| 53 |
{{- range $i, $error := . }} |
| 54 |
// {{ .Constant | funcNameForConstant }} creates a new {{ .Constant }} error: {{ .Description | printf "%q" }} |
| 55 |
func {{ .Constant | funcNameForConstant }}({{ .Description | funcArgsForTags }}) *ApiError { |
| 56 |
eopts := parseOpts(opts) |
| 57 |
if ae, ok := eopts.err.(*ApiError); ok { |
| 58 |
return ae |
| 59 |
} |
| 60 |
{{ if .Description | hasTags }} |
| 61 |
e:=ApiErrors[{{.Constant}}] |
| 62 |
args:=e.toReplacerArgs([]interface{}{ {{.Description | replacerArgsForTags }} }) |
| 63 |
return &ApiError{ |
| 64 |
Code: e.Code, |
| 65 |
ErrCode: e.ErrCode, |
| 66 |
Description: strings.NewReplacer(args...).Replace(e.Description), |
| 67 |
} |
| 68 |
{{- else }} |
| 69 |
return ApiErrors[{{.Constant}}] |
| 70 |
{{- end }} |
| 71 |
} |
| 72 |
|
| 73 |
{{- end }} |
| 74 |
` |
| 75 |
|
| 76 |
func panicIfErr(err error) { |
| 77 |
if err == nil { |
| 78 |
return |
| 79 |
} |
| 80 |
panic(err) |
| 81 |
} |
| 82 |
|
| 83 |
func goFmt(file string) error { |
| 84 |
c := exec.Command("go", "fmt", file) |
| 85 |
out, err := c.CombinedOutput() |
| 86 |
if err != nil { |
| 87 |
log.Printf("go fmt failed: %s", string(out)) |
| 88 |
} |
| 89 |
|
| 90 |
return err |
| 91 |
} |
| 92 |
|
| 93 |
func checkIncrements(errs []server.ErrorsData) error { |
| 94 |
sort.Slice(errs, func(i, j int) bool { |
| 95 |
return errs[i].ErrCode < errs[j].ErrCode |
| 96 |
}) |
| 97 |
|
| 98 |
last := errs[0].ErrCode |
| 99 |
gaps := []uint16{} |
| 100 |
|
| 101 |
for i := 1; i < len(errs); i++ { |
| 102 |
if errs[i].ErrCode != last+1 { |
| 103 |
gaps = append(gaps, last) |
| 104 |
} |
| 105 |
last = errs[i].ErrCode |
| 106 |
} |
| 107 |
|
| 108 |
if len(gaps) > 0 { |
| 109 |
return fmt.Errorf("gaps found in sequences: %v", gaps) |
| 110 |
} |
| 111 |
|
| 112 |
return nil |
| 113 |
} |
| 114 |
|
| 115 |
func checkDupes(errs []server.ErrorsData) error { |
| 116 |
codes := []uint16{} |
| 117 |
highest := uint16(0) |
| 118 |
for _, err := range errs { |
| 119 |
codes = append(codes, err.ErrCode) |
| 120 |
if highest < err.ErrCode { |
| 121 |
highest = err.ErrCode |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
codeKeys := make(map[uint16]bool) |
| 126 |
constKeys := make(map[string]bool) |
| 127 |
|
| 128 |
for _, entry := range errs { |
| 129 |
if _, found := codeKeys[entry.ErrCode]; found { |
| 130 |
return fmt.Errorf("duplicate error code %+v, highest code is %d", entry, highest) |
| 131 |
} |
| 132 |
|
| 133 |
if _, found := constKeys[entry.Constant]; found { |
| 134 |
return fmt.Errorf("duplicate error constant %+v", entry) |
| 135 |
} |
| 136 |
|
| 137 |
codeKeys[entry.ErrCode] = true |
| 138 |
constKeys[entry.Constant] = true |
| 139 |
} |
| 140 |
|
| 141 |
return nil |
| 142 |
} |
| 143 |
|
| 144 |
func findTags(d string) []string { |
| 145 |
tags := []string{} |
| 146 |
for _, tag := range tagRe.FindAllStringSubmatch(d, -1) { |
| 147 |
if len(tag) != 2 { |
| 148 |
continue |
| 149 |
} |
| 150 |
|
| 151 |
tags = append(tags, tag[1]) |
| 152 |
} |
| 153 |
|
| 154 |
sort.Strings(tags) |
| 155 |
|
| 156 |
return tags |
| 157 |
} |
| 158 |
|
| 159 |
func main() { |
| 160 |
ej, err := os.ReadFile("server/errors.json") |
| 161 |
panicIfErr(err) |
| 162 |
|
| 163 |
errs := []server.ErrorsData{} |
| 164 |
panicIfErr(json.Unmarshal(ej, &errs)) |
| 165 |
|
| 166 |
panicIfErr(checkDupes(errs)) |
| 167 |
panicIfErr(checkIncrements(errs)) |
| 168 |
|
| 169 |
sort.Slice(errs, func(i, j int) bool { |
| 170 |
return errs[i].Constant < errs[j].Constant |
| 171 |
}) |
| 172 |
|
| 173 |
t := template.New("errors").Funcs( |
| 174 |
template.FuncMap{ |
| 175 |
"inc": func(i int) int { return i + 1 }, |
| 176 |
"hasTags": func(d string) bool { |
| 177 |
return strings.Contains(d, "{") && strings.Contains(d, "}") |
| 178 |
}, |
| 179 |
"replacerArgsForTags": func(d string) string { |
| 180 |
res := []string{} |
| 181 |
for _, tag := range findTags(d) { |
| 182 |
res = append(res, `"{`+tag+`}"`) |
| 183 |
res = append(res, tag) |
| 184 |
} |
| 185 |
|
| 186 |
return strings.Join(res, ", ") |
| 187 |
}, |
| 188 |
"funcArgsForTags": func(d string) string { |
| 189 |
res := []string{} |
| 190 |
for _, tag := range findTags(d) { |
| 191 |
if tag == "err" { |
| 192 |
res = append(res, "err error") |
| 193 |
} else if tag == "seq" { |
| 194 |
res = append(res, "seq uint64") |
| 195 |
} else { |
| 196 |
res = append(res, fmt.Sprintf("%s interface{}", tag)) |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
res = append(res, "opts ...ErrorOption") |
| 201 |
|
| 202 |
return strings.Join(res, ", ") |
| 203 |
}, |
| 204 |
"funcNameForConstant": func(c string) string { |
| 205 |
res := "" |
| 206 |
|
| 207 |
switch { |
| 208 |
case strings.HasSuffix(c, "ErrF"): |
| 209 |
res = fmt.Sprintf("New%sError", strings.TrimSuffix(c, "ErrF")) |
| 210 |
case strings.HasSuffix(c, "Err"): |
| 211 |
res = fmt.Sprintf("New%sError", strings.TrimSuffix(c, "Err")) |
| 212 |
case strings.HasSuffix(c, "ErrorF"): |
| 213 |
res = fmt.Sprintf("New%s", strings.TrimSuffix(c, "F")) |
| 214 |
case strings.HasSuffix(c, "F"): |
| 215 |
res = fmt.Sprintf("New%sError", strings.TrimSuffix(c, "F")) |
| 216 |
default: |
| 217 |
res = fmt.Sprintf("New%s", c) |
| 218 |
} |
| 219 |
|
| 220 |
if !strings.HasSuffix(res, "Error") { |
| 221 |
res = fmt.Sprintf("%sError", res) |
| 222 |
} |
| 223 |
|
| 224 |
return res |
| 225 |
}, |
| 226 |
}) |
| 227 |
p, err := t.Parse(templ) |
| 228 |
panicIfErr(err) |
| 229 |
|
| 230 |
tf, err := os.CreateTemp("", "") |
| 231 |
panicIfErr(err) |
| 232 |
defer tf.Close() |
| 233 |
|
| 234 |
panicIfErr(p.Execute(tf, errs)) |
| 235 |
|
| 236 |
panicIfErr(os.Rename(tf.Name(), "server/jetstream_errors_generated.go")) |
| 237 |
panicIfErr(goFmt("server/jetstream_errors_generated.go")) |
| 238 |
} |