You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: prefer string codes over integers in examples and docs
- Updated README and examples to use descriptive string identifiers (e.g., USER_CREATED, ERR_NOT_FOUND)
- Updated tests and test resources to use string codes
- Refined documentation to recommend strings for better self-documentation
@@ -268,11 +269,11 @@ After translators fill `translate.es.yaml`, rename or copy it to `es.yaml` for r
268
269
Many projects already use **error or message codes** (HTTP statuses, legacy numeric codes, string identifiers like `ERR_NOT_FOUND`). The optional **`code`** field in the catalog lets you **store that value** with each message and have it returned in `Message.Code` and `ErrorCode()` so your API can expose it unchanged.
269
270
270
271
- **Optional** — You can omit `code` entirely. When empty, use `Message.Key` or `ErrorKey()` as the stable identifier for clients (e.g. in JSON: `"error_code": msg.Code or msg.Key`).
271
-
- **Any value** — Codes are strings. In YAML you can write `code: 404` (parsed as `"404"`) or `code: "ERR_NOT_FOUND"`. In Go use `msgcat.CodeInt(503)` or `msgcat.CodeString("ERR_MAINT")`.
272
+
- **Any value** — Codes are strings. In YAML you can write `code: ERR_NOT_FOUND` or `code: "404"`. In Go use `msgcat.CodeString("ERR_MAINT")` or `msgcat.CodeInt(503)`.
272
273
- **Not unique** — The catalog does not require codes to be unique. If your design uses the same code for several messages (e.g. same HTTP status for different keys), you can repeat the same `code` value.
273
274
- **Your identifier** — The catalog never interprets the code; it only stores and returns it. You decide what values to use and how to expose them in your API.
274
275
275
-
**When to set a code:** Use it when you need a stable, project-specific value to return to clients (status codes, error enums, etc.). When you don’t, leave it unset and use the message **key** as the identifier.
276
+
**When to set a code:** Use it when you need a stable, project-specific value to return to clients (status codes, error enums, etc.). Strings are generally preferred over integers as they are more descriptive (e.g., `code: ERR_ACCESS_DENIED` instead of `code: 403`). When you don’t need a separate code, leave it unset and use the message **key** as the identifier.
276
277
277
278
Helpers for building `RawMessage.Code` in code: `msgcat.CodeInt(503)`, `msgcat.CodeString("ERR_NOT_FOUND")`.
0 commit comments