Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor updates to error data #2255

Merged
merged 1 commit into from May 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion doc/adr/0007-error-codes.md
Expand Up @@ -119,7 +119,8 @@ if IsNatsErr(err, JSStreamNotFoundErr, JSConsumerNotFoundErr) {

## Maintaining the errors

The file `errors.json` holds the data used to generate the error constants, lists etc
The file `server/errors.json` holds the data used to generate the error constants, lists etc. This is JSON versions of
`server.ErrorsData`.

```json
[
Expand All @@ -140,6 +141,9 @@ The file `errors.json` holds the data used to generate the error constants, list
]
```

The `nats` CLI allow you to edit, add and view these files using the `nats errors` command, use the `--errors` flag to
view your local file during development.

After editing this file run `go generate` in the top of the `nats-server` repo, and it will update the needed files. Check
in the result.

Expand Down
4 changes: 2 additions & 2 deletions server/errors.json
Expand Up @@ -108,7 +108,7 @@
"code": 500,
"error_code": 10048,
"description": "{err}",
"commend": "Generic stream assignment error string"
"comment": "Generic stream assignment error string"
},
{
"constant": "JSStreamMessageExceedsMaximumErr",
Expand Down Expand Up @@ -466,7 +466,7 @@
"constant": "JSStreamMsgDeleteFailedF",
"code": 500,
"error_code": 10057,
"description": "%s",
"description": "{err}",
"comment": "Generic message deletion failure error string"
},
{
Expand Down
25 changes: 8 additions & 17 deletions server/errors_gen.go
Expand Up @@ -11,6 +11,8 @@ import (
"os/exec"
"sort"
"text/template"

"github.com/nats-io/nats-server/v2/server"
)

var templ = `
Expand All @@ -32,7 +34,7 @@ const (
var (
ApiErrors = map[ErrorIdentifier]*ApiError{
{{- range $i, $error := . }}
{{ .Constant }}: {Code: {{ .Code }},ErrCode: {{ .ErrCode }},Description: {{ .Description | printf "%q" }},{{- if .Help }}Help: {{ .Help | printf "%q" }},{{- end }}{{- if .URL }}URL: {{ .URL | printf "%q" }},{{- end }} },{{- end }}
{{ .Constant }}: {Code: {{ .Code }},ErrCode: {{ .ErrCode }},Description: {{ .Description | printf "%q" }}},{{- end }}
}

{{- range $i, $error := . }}
Expand All @@ -51,17 +53,6 @@ func panicIfErr(err error) {
panic(err)
}

type Errors struct {
Constant string `json:"constant"`
Code int `json:"code"`
ErrCode int `json:"error_code"`
Description string `json:"description"`
Comment string `json:"comment"`
Help string `json:"help"`
URL string `json:"url"`
Deprecates string `json:"deprecates"`
}

func goFmt(file string) error {
c := exec.Command("go", "fmt", file)
out, err := c.CombinedOutput()
Expand All @@ -72,17 +63,17 @@ func goFmt(file string) error {
return err
}

func checkDupes(errs []Errors) error {
codes := []int{}
highest := 0
func checkDupes(errs []server.ErrorsData) error {
codes := []uint16{}
highest := uint16(0)
for _, err := range errs {
codes = append(codes, err.ErrCode)
if highest < err.ErrCode {
highest = err.ErrCode
}
}

codeKeys := make(map[int]bool)
codeKeys := make(map[uint16]bool)
constKeys := make(map[string]bool)

for _, entry := range errs {
Expand All @@ -105,7 +96,7 @@ func main() {
ej, err := os.ReadFile("server/errors.json")
panicIfErr(err)

errs := []Errors{}
errs := []server.ErrorsData{}
panicIfErr(json.Unmarshal(ej, &errs))
panicIfErr(checkDupes(errs))

Expand Down
14 changes: 12 additions & 2 deletions server/jetstream_errors.go
Expand Up @@ -38,8 +38,18 @@ type ApiError struct {
Code int `json:"code"`
ErrCode uint16 `json:"err_code,omitempty"`
Description string `json:"description,omitempty"`
URL string `json:"-"`
Help string `json:"-"`
}

// ErrorsData is the source data for generated errors as found in errors.json
type ErrorsData struct {
Constant string `json:"constant"`
Code int `json:"code"`
ErrCode uint16 `json:"error_code"`
Description string `json:"description"`
Comment string `json:"comment"`
Help string `json:"help"`
URL string `json:"url"`
Deprecates string `json:"deprecates"`
}

func (e *ApiError) Error() string {
Expand Down
10 changes: 5 additions & 5 deletions server/jetstream_errors_generated.go
Expand Up @@ -138,7 +138,7 @@ const (
// JSStorageResourcesExceededErr insufficient storage resources available
JSStorageResourcesExceededErr ErrorIdentifier = 10047

// JSStreamAssignmentErrF {err}
// JSStreamAssignmentErrF Generic stream assignment error string ({err})
JSStreamAssignmentErrF ErrorIdentifier = 10048

// JSStreamCreateErrF Generic stream creation error string ({err})
Expand Down Expand Up @@ -174,7 +174,7 @@ const (
// JSStreamMismatchErr stream name in subject does not match request
JSStreamMismatchErr ErrorIdentifier = 10056

// JSStreamMsgDeleteFailedF Generic message deletion failure error string (%s)
// JSStreamMsgDeleteFailedF Generic message deletion failure error string ({err})
JSStreamMsgDeleteFailedF ErrorIdentifier = 10057

// JSStreamNameExistErr stream name already in use
Expand Down Expand Up @@ -266,8 +266,8 @@ var (
JSNoAccountErr: {Code: 503, ErrCode: 10035, Description: "account not found"},
JSNoMessageFoundErr: {Code: 404, ErrCode: 10037, Description: "no message found"},
JSNotEmptyRequestErr: {Code: 400, ErrCode: 10038, Description: "expected an empty request payload"},
JSNotEnabledErr: {Code: 503, ErrCode: 10076, Description: "JetStream not enabled", Help: "This error indicates that JetStream is not enabled at a global level"},
JSNotEnabledForAccountErr: {Code: 503, ErrCode: 10039, Description: "JetStream not enabled for account", Help: "This error indicates that JetStream is not enabled for an account account level"},
JSNotEnabledErr: {Code: 503, ErrCode: 10076, Description: "JetStream not enabled"},
JSNotEnabledForAccountErr: {Code: 503, ErrCode: 10039, Description: "JetStream not enabled for account"},
JSPeerRemapErr: {Code: 503, ErrCode: 10075, Description: "peer remap failed"},
JSRaftGeneralErrF: {Code: 500, ErrCode: 10041, Description: "{err}"},
JSRestoreSubscribeFailedErrF: {Code: 500, ErrCode: 10042, Description: "JetStream unable to subscribe to restore snapshot {subject}: {err}"},
Expand All @@ -288,7 +288,7 @@ var (
JSStreamMessageExceedsMaximumErr: {Code: 400, ErrCode: 10054, Description: "message size exceeds maximum allowed"},
JSStreamMirrorNotUpdatableErr: {Code: 400, ErrCode: 10055, Description: "Mirror configuration can not be updated"},
JSStreamMismatchErr: {Code: 400, ErrCode: 10056, Description: "stream name in subject does not match request"},
JSStreamMsgDeleteFailedF: {Code: 500, ErrCode: 10057, Description: "%s"},
JSStreamMsgDeleteFailedF: {Code: 500, ErrCode: 10057, Description: "{err}"},
JSStreamNameExistErr: {Code: 400, ErrCode: 10058, Description: "stream name already in use"},
JSStreamNotFoundErr: {Code: 404, ErrCode: 10059, Description: "stream not found"},
JSStreamNotMatchErr: {Code: 400, ErrCode: 10060, Description: "expected stream does not match"},
Expand Down