From dd8014554ea7fe02ca3fbeee904c5cf7b9be3d41 Mon Sep 17 00:00:00 2001 From: Sauer Date: Mon, 16 Nov 2026 02:36:14 +0100 Subject: [PATCH 1/3] test(sdk): shared conformance vectors for API error messages packages/sdk/conformance/errors.json holds 66 cases of status and raw body with the API's text before any hint, the error code as the TypeScript SDK reads it, and whether a 401 or 404 gets a hint. The TypeScript conformance test writes it and checks the client against it; the Python and Go suites read it from that directory. Each port lists the vectors where it differs on purpose (how it counts the 30-character hint threshold, where it reads a code from), and a listed difference that stops differing fails too. The hand-written tables the vectors now cover are reduced to each SDK's own hint wording. No behaviour change: Python's _plain_error_text and Go's plainErrorText are the existing code moved into a function the tests can call. Co-Authored-By: Claude Opus 5.5 --- packages/sdk-go/client.go | 16 +- packages/sdk-go/client_test.go | 25 +- packages/sdk-go/errors_conformance_test.go | 106 +++ packages/sdk-go/internal_test.go | 16 - packages/sdk-python/src/webhooks_cc/client.py | 19 +- packages/sdk-python/tests/test_client.py | 50 +- .../tests/test_errors_conformance.py | 83 +++ packages/sdk/conformance/README.md | 2 + packages/sdk/conformance/errors.json | 667 ++++++++++++++++++ packages/sdk/src/__tests__/client.test.ts | 77 +- .../sdk/src/__tests__/conformance.test.ts | 603 +++++++++++++++- 11 files changed, 1495 insertions(+), 169 deletions(-) create mode 100644 packages/sdk-go/errors_conformance_test.go create mode 100644 packages/sdk-python/tests/test_errors_conformance.py create mode 100644 packages/sdk/conformance/errors.json diff --git a/packages/sdk-go/client.go b/packages/sdk-go/client.go index 7639d86d2..e055f968e 100644 --- a/packages/sdk-go/client.go +++ b/packages/sdk-go/client.go @@ -576,17 +576,25 @@ func withHint(message, separator, hint string) string { return message + separator + hint } -func errorFromResponse(response *apiResponse, path string) *Error { - message, ok := errorText(response.body) +// plainErrorText is what the API said, as plain text, before any hint: the +// text of a JSON error body (see errorText), else the body itself cut to 200 +// characters. +func plainErrorText(status int, body []byte) string { + message, ok := errorText(body) if !ok { - message = string(response.body) + message = string(body) if utf8.RuneCountInString(message) > 200 { message = string([]rune(message)[:200]) + "..." } } else if bareCode.MatchString(message) { // A bare code ({"error": "not_found"}) is no sentence: say the status (see codeText). - message = codeText(response.status, message) + message = codeText(status, message) } + return message +} + +func errorFromResponse(response *apiResponse, path string) *Error { + message := plainErrorText(response.status, response.body) generic := len(message) < 30 apiErr := &Error{StatusCode: response.status, Message: message} var coded struct { diff --git a/packages/sdk-go/client_test.go b/packages/sdk-go/client_test.go index a78003467..9e0bf2c2a 100644 --- a/packages/sdk-go/client_test.go +++ b/packages/sdk-go/client_test.go @@ -1258,29 +1258,14 @@ func TestErrorMessagesAreThePlainTextOfTheBody(t *testing.T) { body string message string }{ + // What the API said, in every case, is in the shared vectors + // (errors_conformance_test.go); these pin this SDK's own hint wording. {404, `{"error":"Endpoint not found"}`, "Endpoint not found: Use Endpoints.List to see your endpoints"}, - {400, `{"error":"invalid_input","message":"name is too long"}`, "name is too long"}, - {502, "Bad gateway", "Bad gateway"}, - {401, `{"error":"Unauthorized"}`, "Unauthorized. Get an API key at https://webhooks.cc/account/api-keys"}, - {400, `{"error":"invalid_input","message":"` + strings.Repeat("y ", 125) + `"}`, strings.Repeat("y ", 125)}, - {500, strings.Repeat("z", 400), strings.Repeat("z", 200) + "..."}, - // A message only JavaScript's trim() calls blank (U+FEFF) gives way to the error. - {400, `{"message":"\ufeff ","error":"name is too long"}`, "name is too long"}, - // A short message is trimmed as JavaScript trims it before its hint. - {404, "\ufeffNot found\ufeff", "Not found: Use Endpoints.List to see your endpoints"}, - // A bare code is no sentence: the status is said in words instead. {404, `{"error":"not_found"}`, "Not found: Use Endpoints.List to see your endpoints"}, - {404, `{"error":"Request not found"}`, "Request not found: Use Endpoints.List to see your endpoints"}, - {404, `{"error":"not_found","message":"Request not found"}`, "Request not found: Use Endpoints.List to see your endpoints"}, {401, `{"error":"unauthorized"}`, "Unauthorized. Get an API key at https://webhooks.cc/account/api-keys"}, - {403, `{"error":"forbidden"}`, "Forbidden"}, - {409, `{"error":"conflict"}`, "Conflict"}, - // A code that says more than the status stays, in brackets. - {400, `{"error":"invalid_since"}`, "Bad request (invalid_since)"}, - {409, `{"error":"slug_exists"}`, "Conflict (slug_exists)"}, - {500, `{"error":"internal_error"}`, "Server error (internal_error)"}, - {410, `{"error":"expired"}`, "Request failed with HTTP 410 (expired)"}, - {410, `{"message":"expired"}`, "Request failed with HTTP 410 (expired)"}, + // A short message is trimmed as JavaScript trims it before its hint. + {404, "\ufeffNot found\ufeff", "Not found: Use Endpoints.List to see your endpoints"}, + {404, `{"error":" not_found "}`, "not_found: Use Endpoints.List to see your endpoints"}, } for _, c := range cases { api := newFakeAPI(t) diff --git a/packages/sdk-go/errors_conformance_test.go b/packages/sdk-go/errors_conformance_test.go new file mode 100644 index 000000000..f82fbb053 --- /dev/null +++ b/packages/sdk-go/errors_conformance_test.go @@ -0,0 +1,106 @@ +package webhookscc + +import ( + "errors" + "net/http" + "strings" + "testing" + + "webhooks.cc/go/internal/conformance" + "webhooks.cc/go/internal/webtext" +) + +// Where this SDK differs from the TypeScript one on purpose, by vector name. +var ( + // Go counts bytes for the 30-character hint threshold, TypeScript UTF-16 + // units, so a short message with non-ASCII text can get no hint here. + errorHintDiffers = map[string]string{ + "404 20 accented letters": "20 letters are 40 bytes, over 30 here; TypeScript counts 20 UTF-16 units", + } + // Go reads a code only from "error", wherever it is in the body; + // TypeScript only from a leading "error" that is a code, or a "code" field. + errorCodeDiffers = map[string]string{ + "502 a code field beside a sentence": "the code field is not read", + "429 search_busy as a code field": "the code field is not read", + "400 message before invalid_cursor": "error is read wherever it is in the body", + } +) + +func TestErrorsConformance(t *testing.T) { + var file struct { + Version int `json:"version"` + Vectors []struct { + Name string `json:"name"` + Status int `json:"status"` + Body string `json:"body"` + Expected struct { + Message string `json:"message"` + Code *string `json:"code"` + Hinted bool `json:"hinted"` + } `json:"expected"` + } `json:"vectors"` + } + conformance.Load(t, "errors.json", &file) + if file.Version != 1 { + t.Fatalf("errors.json version %d; this test reads version 1", file.Version) + } + if len(file.Vectors) == 0 { + t.Fatal("no vectors") + } + names := map[string]bool{} + for _, vector := range file.Vectors { + names[vector.Name] = true + t.Run(vector.Name, func(t *testing.T) { + status, body, want := vector.Status, []byte(vector.Body), vector.Expected + plain := plainErrorText(status, body) + if plain != want.Message { + t.Fatalf("message %q, want %q", plain, want.Message) + } + + apiErr := errorFromResponse(&apiResponse{status: status, header: http.Header{}, body: body}, "/endpoints/acme") + if apiErr.StatusCode != status { + t.Errorf("status %d", apiErr.StatusCode) + } + code := "" + if want.Code != nil { + code = *want.Code + } + codeAgrees := (want.Code == nil || apiErr.code == code) && + errors.Is(apiErr, ErrInvalidCursor) == (status == http.StatusBadRequest && code == "invalid_cursor") && + isBusySearch(apiErr) == (status == http.StatusTooManyRequests && code == "search_busy") + // A listed difference that no longer differs fails too, so the list stays true. + if _, differs := errorCodeDiffers[vector.Name]; codeAgrees == differs { + t.Errorf("code %q agrees %v, listed as a difference %v", apiErr.code, codeAgrees, differs) + } + + // A 429's message is this SDK's own rate-limit text. + if status == http.StatusTooManyRequests { + return + } + hinted := (status == http.StatusUnauthorized || status == http.StatusNotFound) && apiErr.Message != plain + if _, differs := errorHintDiffers[vector.Name]; differs { + if hinted == want.Hinted { + t.Errorf("hinted %v, listed as a difference", hinted) + } + return + } + if hinted != want.Hinted { + t.Fatalf("hinted %v, want %v (message %q)", hinted, want.Hinted, apiErr.Message) + } + // Go trims the message, as JavaScript's trim() does, before its hint. + if hinted && !strings.HasPrefix(apiErr.Message, webtext.JSTrim(plain)) { + t.Errorf("message %q does not start with %q", apiErr.Message, plain) + } + if !hinted && apiErr.Message != plain { + t.Errorf("message %q, want %q", apiErr.Message, plain) + } + }) + } + for _, list := range []map[string]string{errorHintDiffers, errorCodeDiffers} { + for name := range list { + if !names[name] { + t.Errorf("no vector named %q", name) + } + } + } +} diff --git a/packages/sdk-go/internal_test.go b/packages/sdk-go/internal_test.go index 2cff50494..a746245c1 100644 --- a/packages/sdk-go/internal_test.go +++ b/packages/sdk-go/internal_test.go @@ -316,22 +316,6 @@ func TestSearchCollectsMoreThanAPageHolds(t *testing.T) { } } -// A bare code gives way to the status in words, and stays the error's code. -func TestABareCodeIsKeptAsTheCode(t *testing.T) { - cases := []struct { - body, message, code string - }{ - {`{"error":"not_found"}`, "Not found: Use Endpoints.List to see your endpoints", "not_found"}, - {`{"error":"not_found","message":"Request not found"}`, "Request not found: Use Endpoints.List to see your endpoints", "not_found"}, - } - for _, c := range cases { - err := errorFromResponse(&apiResponse{status: http.StatusNotFound, body: []byte(c.body)}, "/requests/r1") - if err.Message != c.message || err.code != c.code { - t.Errorf("%s: message %q, code %q", c.body, err.Message, err.code) - } - } -} - // An error on a later page is the call's error, never a short result. func TestAnErrorOnALaterPageIsReturned(t *testing.T) { ctx := context.Background() diff --git a/packages/sdk-python/src/webhooks_cc/client.py b/packages/sdk-python/src/webhooks_cc/client.py index 72e090062..d7bc5c96c 100644 --- a/packages/sdk-python/src/webhooks_cc/client.py +++ b/packages/sdk-python/src/webhooks_cc/client.py @@ -336,18 +336,25 @@ def _error_text(body: str) -> str | None: return None +def _plain_error_text(status: int, text: str) -> str: + """What the API said, as plain text, before any hint: the text of a JSON + error body (see ``_error_text``), else the body itself cut to 200 characters.""" + plain = _error_text(text) + if plain is None: + return text[:200] + "..." if len(text) > 200 else text + if _BARE_CODE.fullmatch(plain): + # A bare code ({"error": "not_found"}) is no sentence: say the status (see _code_text). + return _code_text(status, plain) + return plain + + def _error_from_response(response: httpx.Response) -> WebhooksCCError: text = response.text try: path = response.request.url.path except RuntimeError: # a response built without its request path = "" - plain = _error_text(text) - if plain is None: - plain = text[:200] + "..." if len(text) > 200 else text - elif _BARE_CODE.fullmatch(plain): - # A bare code ({"error": "not_found"}) is no sentence: say the status (see _code_text). - plain = _code_text(response.status_code, plain) + plain = _plain_error_text(response.status_code, text) return _map_error(response.status_code, plain, response.headers, path, text) diff --git a/packages/sdk-python/tests/test_client.py b/packages/sdk-python/tests/test_client.py index d5bc16b1d..769508fa2 100644 --- a/packages/sdk-python/tests/test_client.py +++ b/packages/sdk-python/tests/test_client.py @@ -851,6 +851,8 @@ def test_errors_are_typed(status: int, error_type: type[Exception]) -> None: assert caught.value.status_code == status +# What the API said, in every case, is in the shared vectors +# (test_errors_conformance.py); these pin this SDK's own hint wording. @pytest.mark.parametrize( ("status", "body", "message"), [ @@ -859,69 +861,23 @@ def test_errors_are_typed(status: int, error_type: type[Exception]) -> None: '{"error":"Endpoint not found"}', "Endpoint not found: use client.endpoints.list() to see your endpoints", ), - ( - 400, - '{"error":"invalid_input","message":"name is too long"}', - "name is too long", - ), - (502, "Bad gateway", "Bad gateway"), - (400, json.dumps({"error": "invalid_input", "message": "y " * 125}), "y " * 125), - (500, "z" * 400, "z" * 200 + "..."), - # A message only JavaScript's trim() calls blank (U+FEFF) gives way to the error. - (400, '{"message":"\\ufeff ","error":"name is too long"}', "name is too long"), - # A bare code is no sentence: the status is said in words instead. ( 404, '{"error":"not_found"}', "Not found: use client.endpoints.list() to see your endpoints", ), - ( - 404, - '{"error":"Request not found"}', - "Request not found: use client.endpoints.list() to see your endpoints", - ), - ( - 404, - '{"error":"not_found","message":"Request not found"}', - "Request not found: use client.endpoints.list() to see your endpoints", - ), ( 401, '{"error":"unauthorized"}', "Unauthorized. Get an API key at https://webhooks.cc/account/api-keys", ), - (403, '{"error":"forbidden"}', "Forbidden"), - (409, '{"error":"conflict"}', "Conflict"), - # A code that says more than the status stays, in brackets. - (400, '{"error":"invalid_since"}', "Bad request (invalid_since)"), - (409, '{"error":"slug_exists"}', "Conflict (slug_exists)"), - (500, '{"error":"internal_error"}', "Server error (internal_error)"), - (410, '{"error":"expired"}', "Request failed with HTTP 410 (expired)"), - (410, '{"message":"expired"}', "Request failed with HTTP 410 (expired)"), ], ) -def test_error_messages_are_the_plain_text_of_the_body( - status: int, body: str, message: str -) -> None: +def test_short_401_and_404_messages_get_a_hint(status: int, body: str, message: str) -> None: client = make_client(lambda request: httpx.Response(status, text=body)) with pytest.raises(errors.WebhooksCCError) as caught: client.endpoints.get("acme") assert caught.value.message == message - assert not caught.value.message.startswith("{") - - -def test_a_bare_code_keeps_its_meaning() -> None: - cursor = make_client(lambda request: httpx.Response(400, json={"error": "invalid_cursor"})) - with pytest.raises(errors.InvalidCursorError): - cursor.endpoints.get("acme") - busy = make_client(lambda request: httpx.Response(429, json={"error": "search_busy"})) - with pytest.raises(errors.RateLimitError) as caught: - busy.endpoints.get("acme") - assert caught.value.code == "search_busy" - plain = make_client(lambda request: httpx.Response(400, json={"error": "invalid_input"})) - with pytest.raises(errors.WebhooksCCError) as other: - plain.endpoints.get("acme") - assert other.value.message == "Bad request (invalid_input)" def test_rate_limit_error_carries_the_headers() -> None: diff --git a/packages/sdk-python/tests/test_errors_conformance.py b/packages/sdk-python/tests/test_errors_conformance.py new file mode 100644 index 000000000..5c21c1e22 --- /dev/null +++ b/packages/sdk-python/tests/test_errors_conformance.py @@ -0,0 +1,83 @@ +"""Error messages made from API answers, against the shared vectors.""" + +from __future__ import annotations + +from typing import Any + +import httpx +import pytest + +from tests.conformance import load, vector_id +from webhooks_cc import WebhooksCC, errors +from webhooks_cc.client import _error_code, _plain_error_text + +VECTORS = load("errors")["vectors"] + +# Where this SDK differs from the TypeScript one on purpose, by vector name. + +# Python counts code points for the 30-character hint threshold, TypeScript +# UTF-16 units, so 20 emoji (40 units) are short here and get a hint. +HINT_DIFFERS = { + "404 20 emoji": "20 code points are under 30 here; TypeScript counts 40 UTF-16 units", +} + +# Python reads a code only from ``error``, wherever it is in the body and +# whatever it holds; TypeScript only from a leading ``error`` that is a code, +# or a ``code`` field. +CODE_DIFFERS = { + "502 a code field beside a sentence": "the code field is not read", + "429 search_busy as a code field": "the code field is not read", + "400 message before invalid_cursor": "error is read wherever it is in the body", + "429 a sentence": "RateLimitError.code is the error as sent, a sentence included", +} + + +def _raise(vector: dict[str, Any]) -> errors.WebhooksCCError: + status, body = vector["status"], vector["body"] + client = WebhooksCC( + "whcc_conformance", + http_client=httpx.Client( + transport=httpx.MockTransport(lambda request: httpx.Response(status, text=body)) + ), + ) + with pytest.raises(errors.WebhooksCCError) as caught: + client.endpoints.get("acme") + return caught.value + + +@pytest.mark.parametrize("vector", VECTORS, ids=vector_id) +def test_error_vector(vector: dict[str, Any]) -> None: + status, body, expected = vector["status"], vector["body"], vector["expected"] + plain = _plain_error_text(status, body) + assert plain == expected["message"] + + error = _raise(vector) + assert error.status_code == status + cursor = isinstance(error, errors.InvalidCursorError) + name = vector["name"] + code_agrees = ( + (expected["code"] is None or _error_code(body) == expected["code"]) + and cursor == (status == 400 and expected["code"] == "invalid_cursor") + and (not isinstance(error, errors.RateLimitError) or error.code == expected["code"]) + ) + # A listed difference that no longer differs fails too, so the list stays true. + assert code_agrees == (name not in CODE_DIFFERS) + + # 429 and an invalid cursor have messages of their own. + if status == 429 or cursor: + return + hinted = status in (401, 404) and error.message != plain + if name in HINT_DIFFERS: + assert hinted != expected["hinted"] + return + assert hinted == expected["hinted"] + if hinted: + assert error.message.startswith(plain) + else: + assert error.message == plain + + +def test_the_lists_of_differences_name_real_vectors() -> None: + names = {vector["name"] for vector in VECTORS} + assert set(HINT_DIFFERS) <= names + assert set(CODE_DIFFERS) <= names diff --git a/packages/sdk/conformance/README.md b/packages/sdk/conformance/README.md index 1d046d4c8..815666e96 100644 --- a/packages/sdk/conformance/README.md +++ b/packages/sdk/conformance/README.md @@ -9,6 +9,7 @@ Test data every webhooks.cc SDK must pass. The TypeScript SDK in this package is | `forwarding.json` | Verify what webhooks.cc forwards (`verifyForwardedEmail`, `verifyForwardedRequest`, `verifyForwardedJson`, `verifyRelayedRequest`), with the same error codes, and refuse a secret with no key. | | `matchers.json` | Match captured requests (`matchHeader`, `matchBodyPath`, `matchEmail`, ...), as `waitFor` does. | | `durations.json` | Read timeouts and poll intervals given as `"30s"`, `"5m"` or milliseconds. Only for ports that take durations that way (Go does not). | +| `errors.json` | Turn an API error answer (status and body) into the error's text before any hint, say whether a 401 or 404 gets the SDK's hint, and read the error code. | | `rules.json` | Evaluate conditions of response rules and forwarding filters. The receiver runs these (Rust); a port needs them only if it evaluates conditions itself. | | `agent-credentials.json` | Find, read and write the agent credential file the MCP server, the SDKs and the CLI share (`agent-credentials.md`). Written by `src/__tests__/agent-credentials.test.ts`. | | `placeholders.json` | Fill in `{{ ... }}` placeholders in mock responses. The receiver runs these (Rust); a port needs them only if it renders templates itself. | @@ -23,6 +24,7 @@ Each file has a `version` (raised when the format changes), a `description` that - HubSpot signs the method, the URL, the body and the timestamp in milliseconds. In the URL, exactly these escapes are decoded first, and only in uppercase: `%3A %2F %3F %40 %21 %24 %27 %28 %29 %2A %2C %3B`. HubSpot is the only provider whose signatures expire (after five minutes); the others verify at any age, so captured requests can be checked later. - PayPal: a certificate is trusted because of where it comes from, not by its chain or dates. Only fetch `https` URLs on `api.paypal.com`, `api-m.paypal.com`, `api.sandbox.paypal.com` or `api-m.sandbox.paypal.com` whose path starts with `/v1/notifications/certs/`; refuse anything else, and any `paypal-auth-algo` other than `SHA256withRSA`. Read the URL the way browsers do (a `\` is a `/`), then download the URL rebuilt from what you checked, never the raw header: an HTTP client that parses `https://api.paypal.com\v1\notifications\certs\x@evil.test/` its own way fetches from `evil.test`, and the sender's certificate then makes a forged signature valid. Redirects get the same check. The signed message is `transmission id|transmission time|webhook id|CRC-32 of the body as a decimal number`. A port should let tests supply the certificate instead of downloading it, as the vectors do. - Header names match in any case. +- Error text: compare the API's text before the hint (`expected.message`), never the hint's wording, which is each SDK's own. A 429's message is each SDK's rate-limit text. Where a port legitimately differs (how it counts the 30 characters below which a hint is added, where it reads a code from, an error class with a message of its own), its test lists the vector and the reason. ## Changing behaviour diff --git a/packages/sdk/conformance/errors.json b/packages/sdk/conformance/errors.json new file mode 100644 index 000000000..aec5c09da --- /dev/null +++ b/packages/sdk/conformance/errors.json @@ -0,0 +1,667 @@ +{ + "version": 1, + "generatedBy": "packages/sdk/src/__tests__/conformance.test.ts", + "description": "Error messages made from API answers. A call (GET /api/endpoints/acme) answered with status and body (exactly these characters, as UTF-8) fails with an error whose text before any hint the SDK adds is expected.message: the body's string message, else its string error (a blank one, as JavaScript's trim() sees it, gives way), cut to 1000 characters with ... after it; a message that is only a code (lower-case ASCII letters, digits and underscores, starting with a letter) becomes the status in words, alone when the code only names the status, else with the code in brackets; a body that is not a JSON object with either is kept as it came, cut to 200 characters. Characters are code points, so a cut never splits a surrogate pair. expected.hinted says whether the SDK adds its recovery hint, which only a 401 or 404 message under 30 characters (UTF-16 units) gets; the hint's wording is each SDK's own. On any status but 401, 404 and 429 (whose message is each SDK's rate-limit text) the error's message is expected.message itself. expected.code is the API's error code as the TypeScript SDK reads it: error when it is the body's first key and a code, else a code field that is a code, else null. A port lists in its test the vectors where it differs, and why.", + "vectors": [ + { + "name": "404 not_found", + "status": 404, + "body": "{\"error\":\"not_found\"}", + "expected": { + "message": "Not found", + "code": "not_found", + "hinted": true + } + }, + { + "name": "401 unauthorized", + "status": 401, + "body": "{\"error\":\"unauthorized\"}", + "expected": { + "message": "Unauthorized", + "code": "unauthorized", + "hinted": true + } + }, + { + "name": "403 forbidden", + "status": 403, + "body": "{\"error\":\"forbidden\"}", + "expected": { + "message": "Forbidden", + "code": "forbidden", + "hinted": false + } + }, + { + "name": "409 conflict", + "status": 409, + "body": "{\"error\":\"conflict\"}", + "expected": { + "message": "Conflict", + "code": "conflict", + "hinted": false + } + }, + { + "name": "400 bad_request", + "status": 400, + "body": "{\"error\":\"bad_request\"}", + "expected": { + "message": "Bad request", + "code": "bad_request", + "hinted": false + } + }, + { + "name": "429 too_many_requests", + "status": 429, + "body": "{\"error\":\"too_many_requests\"}", + "expected": { + "message": "Too many requests", + "code": "too_many_requests", + "hinted": false + } + }, + { + "name": "500 server_error", + "status": 500, + "body": "{\"error\":\"server_error\"}", + "expected": { + "message": "Server error", + "code": "server_error", + "hinted": false + } + }, + { + "name": "410 names its own status words", + "status": 410, + "body": "{\"error\":\"request_failed_with_http_410\"}", + "expected": { + "message": "Request failed with HTTP 410", + "code": "request_failed_with_http_410", + "hinted": false + } + }, + { + "name": "400 invalid_since", + "status": 400, + "body": "{\"error\":\"invalid_since\"}", + "expected": { + "message": "Bad request (invalid_since)", + "code": "invalid_since", + "hinted": false + } + }, + { + "name": "400 invalid_cursor", + "status": 400, + "body": "{\"error\":\"invalid_cursor\"}", + "expected": { + "message": "Bad request (invalid_cursor)", + "code": "invalid_cursor", + "hinted": false + } + }, + { + "name": "409 slug_exists", + "status": 409, + "body": "{\"error\":\"slug_exists\"}", + "expected": { + "message": "Conflict (slug_exists)", + "code": "slug_exists", + "hinted": false + } + }, + { + "name": "401 invalid_token", + "status": 401, + "body": "{\"error\":\"invalid_token\"}", + "expected": { + "message": "Unauthorized (invalid_token)", + "code": "invalid_token", + "hinted": true + } + }, + { + "name": "401 invalid_api_key is 30 characters, no hint", + "status": 401, + "body": "{\"error\":\"invalid_api_key\"}", + "expected": { + "message": "Unauthorized (invalid_api_key)", + "code": "invalid_api_key", + "hinted": false + } + }, + { + "name": "404 a code with digits", + "status": 404, + "body": "{\"error\":\"e404\"}", + "expected": { + "message": "Not found (e404)", + "code": "e404", + "hinted": true + } + }, + { + "name": "400 a code with digits and an underscore", + "status": 400, + "body": "{\"error\":\"v2_bad\"}", + "expected": { + "message": "Bad request (v2_bad)", + "code": "v2_bad", + "hinted": false + } + }, + { + "name": "429 search_busy", + "status": 429, + "body": "{\"error\":\"search_busy\"}", + "expected": { + "message": "Too many requests (search_busy)", + "code": "search_busy", + "hinted": false + } + }, + { + "name": "500 internal_error", + "status": 500, + "body": "{\"error\":\"internal_error\"}", + "expected": { + "message": "Server error (internal_error)", + "code": "internal_error", + "hinted": false + } + }, + { + "name": "503 unavailable", + "status": 503, + "body": "{\"error\":\"unavailable\"}", + "expected": { + "message": "Server error (unavailable)", + "code": "unavailable", + "hinted": false + } + }, + { + "name": "410 expired", + "status": 410, + "body": "{\"error\":\"expired\"}", + "expected": { + "message": "Request failed with HTTP 410 (expired)", + "code": "expired", + "hinted": false + } + }, + { + "name": "418 teapot", + "status": 418, + "body": "{\"error\":\"im_a_teapot\"}", + "expected": { + "message": "Request failed with HTTP 418 (im_a_teapot)", + "code": "im_a_teapot", + "hinted": false + } + }, + { + "name": "410 a message that is only a code", + "status": 410, + "body": "{\"message\":\"expired\"}", + "expected": { + "message": "Request failed with HTTP 410 (expired)", + "code": null, + "hinted": false + } + }, + { + "name": "409 a message that is only a code, beside another code", + "status": 409, + "body": "{\"error\":\"invalid_input\",\"message\":\"name_taken\"}", + "expected": { + "message": "Conflict (name_taken)", + "code": "invalid_input", + "hinted": false + } + }, + { + "name": "400 a 1000-character code is whole", + "status": 400, + "body": "{\"error\":\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "hinted": false + } + }, + { + "name": "404 a bare number is no code", + "status": 404, + "body": "{\"error\":\"404\"}", + "expected": { + "message": "404", + "code": null, + "hinted": true + } + }, + { + "name": "404 a hyphen is no code", + "status": 404, + "body": "{\"error\":\"not-found\"}", + "expected": { + "message": "not-found", + "code": null, + "hinted": true + } + }, + { + "name": "404 a capital letter is no code", + "status": 404, + "body": "{\"error\":\"Not_found\"}", + "expected": { + "message": "Not_found", + "code": null, + "hinted": true + } + }, + { + "name": "404 a letter past ASCII is no code", + "status": 404, + "body": "{\"error\":\"nöt_found\"}", + "expected": { + "message": "nöt_found", + "code": null, + "hinted": true + } + }, + { + "name": "404 a padded code is no code", + "status": 404, + "body": "{\"error\":\" not_found \"}", + "expected": { + "message": " not_found ", + "code": null, + "hinted": true + } + }, + { + "name": "404 a code with a line break after it is no code", + "status": 404, + "body": "{\"error\":\"not_found\\n\"}", + "expected": { + "message": "not_found\n", + "code": null, + "hinted": true + } + }, + { + "name": "404 a sentence", + "status": 404, + "body": "{\"error\":\"Request not found\"}", + "expected": { + "message": "Request not found", + "code": null, + "hinted": true + } + }, + { + "name": "429 a sentence", + "status": 429, + "body": "{\"error\":\"Rate limit exceeded\"}", + "expected": { + "message": "Rate limit exceeded", + "code": null, + "hinted": false + } + }, + { + "name": "503 a sentence", + "status": 503, + "body": "{\"error\":\"Service unavailable\"}", + "expected": { + "message": "Service unavailable", + "code": null, + "hinted": false + } + }, + { + "name": "404 message beside a code", + "status": 404, + "body": "{\"error\":\"not_found\",\"message\":\"Request not found\"}", + "expected": { + "message": "Request not found", + "code": "not_found", + "hinted": true + } + }, + { + "name": "400 message beside invalid_cursor", + "status": 400, + "body": "{\"error\":\"invalid_cursor\",\"message\":\"The cursor is not one this search returned\"}", + "expected": { + "message": "The cursor is not one this search returned", + "code": "invalid_cursor", + "hinted": false + } + }, + { + "name": "400 message before invalid_cursor", + "status": 400, + "body": "{\"message\":\"The cursor is not one this search returned\",\"error\":\"invalid_cursor\"}", + "expected": { + "message": "The cursor is not one this search returned", + "code": null, + "hinted": false + } + }, + { + "name": "409 message beside a sentence", + "status": 409, + "body": "{\"message\":\"Name is taken\",\"error\":\"Conflict\"}", + "expected": { + "message": "Name is taken", + "code": null, + "hinted": false + } + }, + { + "name": "400 a blank message gives way", + "status": 400, + "body": "{\"message\":\" \",\"error\":\"name is too long\"}", + "expected": { + "message": "name is too long", + "code": null, + "hinted": false + } + }, + { + "name": "400 a message only trim() calls blank (U+FEFF) gives way", + "status": 400, + "body": "{\"message\":\"\\ufeff \",\"error\":\"name is too long\"}", + "expected": { + "message": "name is too long", + "code": null, + "hinted": false + } + }, + { + "name": "404 a message that is not a string gives way", + "status": 404, + "body": "{\"message\":5,\"error\":\"Endpoint not found\"}", + "expected": { + "message": "Endpoint not found", + "code": null, + "hinted": true + } + }, + { + "name": "400 a message over 200 characters is read whole", + "status": 400, + "body": "{\"error\":\"invalid_input\",\"message\":\"y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y \"}", + "expected": { + "message": "y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y ", + "code": "invalid_input", + "hinted": false + } + }, + { + "name": "400 a message over 1000 characters is cut", + "status": 400, + "body": "{\"message\":\"mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm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input", + "hinted": false + } + }, + { + "name": "502 a code field beside a sentence", + "status": 502, + "body": "{\"error\":\"Polar did not answer\",\"code\":\"outcome_unknown\"}", + "expected": { + "message": "Polar did not answer", + "code": "outcome_unknown", + "hinted": false + } + }, + { + "name": "429 search_busy as a code field", + "status": 429, + "body": "{\"error\":\"Search busy\",\"code\":\"search_busy\"}", + "expected": { + "message": "Search busy", + "code": "search_busy", + "hinted": false + } + }, + { + "name": "400 a code field that is no code", + "status": 400, + "body": "{\"error\":\"Bad things\",\"code\":\"Not-A-Code\"}", + "expected": { + "message": "Bad things", + "code": null, + "hinted": false + } + }, + { + "name": "502 plain text", + "status": 502, + "body": "Bad gateway", + "expected": { + "message": "Bad gateway", + "code": null, + "hinted": false + } + }, + { + "name": "404 plain text that looks like a code", + "status": 404, + "body": "not_found", + "expected": { + "message": "not_found", + "code": null, + "hinted": true + } + }, + { + "name": "404 a JSON string", + "status": 404, + "body": "\"not_found\"", + "expected": { + "message": "\"not_found\"", + "code": null, + "hinted": true + } + }, + { + "name": "404 a JSON array", + "status": 404, + "body": "[\"not_found\"]", + "expected": { + "message": "[\"not_found\"]", + "code": null, + "hinted": true + } + }, + { + "name": "404 JSON null", + "status": 404, + "body": "null", + "expected": { + "message": "null", + "code": null, + "hinted": true + } + }, + { + "name": "400 an error that is an object", + "status": 400, + "body": "{\"error\":{\"code\":\"x\"}}", + "expected": { + "message": "{\"error\":{\"code\":\"x\"}}", + "code": null, + "hinted": false + } + }, + { + "name": "400 empty message and error", + "status": 400, + "body": "{\"message\":\"\",\"error\":\"\"}", + "expected": { + "message": "{\"message\":\"\",\"error\":\"\"}", + "code": null, + "hinted": false + } + }, + { + "name": "400 an object without either", + "status": 400, + "body": "{\"detail\":\"x\"}", + "expected": { + "message": "{\"detail\":\"x\"}", + "code": null, + "hinted": false + } + }, + { + "name": "503 HTML", + "status": 503, + "body": "Service Unavailable", + "expected": { + "message": "Service Unavailable", + "code": null, + "hinted": false + } + }, + { + "name": "404 an empty body", + "status": 404, + "body": "", + "expected": { + "message": "", + "code": null, + "hinted": true + } + }, + { + "name": "500 a 200-character body is whole", + "status": 500, + "body": "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", + "expected": { + "message": "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", + "code": null, + "hinted": false + } + }, + { + "name": "500 a long body is cut", + "status": 500, + "body": "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", + "expected": { + "message": "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz...", + "code": null, + "hinted": false + } + }, + { + "name": "502 a cut body never splits a surrogate pair", + "status": 502, + "body": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀", + "expected": { + "message": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb😀...", + "code": null, + "hinted": false + } + }, + { + "name": "400 unterminated JSON is plain text", + "status": 400, + "body": "{\"error\":\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "expected": { + "message": "{\"error\":\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...", + "code": null, + "hinted": false + } + }, + { + "name": "404 29 characters get a hint", + "status": 404, + "body": "{\"error\":\"This endpoint does not exist.\"}", + "expected": { + "message": "This endpoint does not exist.", + "code": null, + "hinted": true + } + }, + { + "name": "404 30 characters get none", + "status": 404, + "body": "{\"error\":\"This endpoint does not exist!!\"}", + "expected": { + "message": "This endpoint does not exist!!", + "code": null, + "hinted": false + } + }, + { + "name": "401 a short sentence", + "status": 401, + "body": "{\"error\":\"Invalid API key\"}", + "expected": { + "message": "Invalid API key", + "code": null, + "hinted": true + } + }, + { + "name": "401 a long sentence", + "status": 401, + "body": "{\"error\":\"This API key was revoked by its owner.\"}", + "expected": { + "message": "This API key was revoked by its owner.", + "code": null, + "hinted": false + } + }, + { + "name": "404 20 accented letters", + "status": 404, + "body": "{\"error\":\"éééééééééééééééééééé\"}", + "expected": { + "message": "éééééééééééééééééééé", + "code": null, + "hinted": true + } + }, + { + "name": "404 20 emoji", + "status": 404, + "body": "{\"error\":\"😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀\"}", + "expected": { + "message": "😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀", + "code": null, + "hinted": false + } + } + ] +} diff --git a/packages/sdk/src/__tests__/client.test.ts b/packages/sdk/src/__tests__/client.test.ts index 42bbee115..175e3d7cd 100644 --- a/packages/sdk/src/__tests__/client.test.ts +++ b/packages/sdk/src/__tests__/client.test.ts @@ -2660,16 +2660,8 @@ describe("WebhooksCC", () => { expect(error.code).toBeUndefined(); }); - it("prefers a JSON body's message to its error code", async () => { - const error = await failWith( - 400, - '{"error":"invalid_cursor","message":"The cursor is not one this search returned"}' - ); - expect(error.message).toBe("The cursor is not one this search returned"); - expect(error.apiMessage).toBe("The cursor is not one this search returned"); - expect(error.code).toBe("invalid_cursor"); - }); - + // What the API said, in every case, is in conformance/errors.json + // (conformance.test.ts); this pins the hint a bare code gets. it("says the status in words when the body names only a code", async () => { const error = await failWith(404, '{"error":"not_found"}'); expect(error).toBeInstanceOf(NotFoundError); @@ -2678,59 +2670,6 @@ describe("WebhooksCC", () => { "Not found: Use client.endpoints.list() to see available endpoints." ); expect(error.code).toBe("not_found"); - for (const [status, code, text] of [ - [401, "unauthorized", "Unauthorized"], - [403, "forbidden", "Forbidden"], - [409, "conflict", "Conflict"], - [429, "too_many_requests", "Too many requests"], - [400, "invalid_since", "Bad request (invalid_since)"], - [409, "slug_exists", "Conflict (slug_exists)"], - [500, "internal_error", "Server error (internal_error)"], - [503, "unavailable", "Server error (unavailable)"], - [410, "expired", "Request failed with HTTP 410 (expired)"], - ] as const) { - const coded = await failWith(status, JSON.stringify({ error: code })); - expect(coded.apiMessage, String(status)).toBe(text); - expect(coded.code, String(status)).toBe(code); - } - // A message that is only a code keeps it too. - const message = await failWith(410, '{"message":"expired"}'); - expect(message.apiMessage).toBe("Request failed with HTTP 410 (expired)"); - }); - - it("keeps an error that is a sentence, and a message beside a code", async () => { - const sentence = await failWith(404, '{"error":"Request not found"}'); - expect(sentence.apiMessage).toBe("Request not found"); - expect(sentence.code).toBeUndefined(); - const both = await failWith(404, '{"error":"not_found","message":"Request not found"}'); - expect(both.apiMessage).toBe("Request not found"); - expect(both.code).toBe("not_found"); - }); - - it("lets a message only trim() calls blank (U+FEFF) give way to the error", async () => { - const error = await failWith(400, '{"message":"\\ufeff ","error":"name is too long"}'); - expect(error.apiMessage).toBe("name is too long"); - }); - - it("keeps a body that is not JSON as it came", async () => { - const error = await failWith(502, "Bad gateway"); - expect(error.apiMessage).toBe("Bad gateway"); - expect(error.message).toBe("Bad gateway"); - }); - - it("reads the whole JSON body even when it is over 200 characters", async () => { - const long = "y ".repeat(125); - const error = await failWith(400, JSON.stringify({ error: "invalid_input", message: long })); - expect(error.message).toBe(long); - expect(error.message.startsWith("{")).toBe(false); - expect(error.code).toBe("invalid_input"); - }); - - it("never splits a surrogate pair when it cuts a long message", async () => { - // 999 plain characters, then emoji: the cut falls on the first emoji. - const message = "a".repeat(999) + "\u{1F600}".repeat(5); - const error = await failWith(400, JSON.stringify({ error: "invalid_input", message })); - expect(error.apiMessage).toBe("a".repeat(999) + "\u{1F600}..."); }); it("names the stream's path on a refused stream", async () => { @@ -2746,18 +2685,6 @@ describe("WebhooksCC", () => { expect(error.path).toBe("/stream/acme"); expect(error.apiMessage).toBe("Endpoint not found"); }); - - it("cuts a long body that is not JSON by code point, never splitting an emoji", async () => { - // 199 plain characters, then emoji: the cut at 200 falls on the first emoji. - const error = await failWith(502, "b".repeat(199) + "\u{1F600}".repeat(50)); - expect(error.apiMessage).toBe("b".repeat(199) + "\u{1F600}..."); - expect(error.message).toBe("b".repeat(199) + "\u{1F600}..."); - }); - - it("cuts a long body that is not JSON short", async () => { - const error = await failWith(500, "z".repeat(400)); - expect(error.apiMessage).toBe("z".repeat(200) + "..."); - }); }); describe("retry", () => { diff --git a/packages/sdk/src/__tests__/conformance.test.ts b/packages/sdk/src/__tests__/conformance.test.ts index 74913322e..887d05deb 100644 --- a/packages/sdk/src/__tests__/conformance.test.ts +++ b/packages/sdk/src/__tests__/conformance.test.ts @@ -11,7 +11,8 @@ */ import { createHmac } from "node:crypto"; import { afterEach, describe, expect, it, vi } from "vitest"; -import { WebhookVerificationError } from "../errors"; +import { WebhooksCC } from "../client"; +import { WebhookVerificationError, WebhooksCCError } from "../errors"; import { isEmailRequest, matchAll, @@ -1525,6 +1526,581 @@ function buildDurationVectors() { }); } +// --------------------------------------------------------------------------- +// Error messages +// --------------------------------------------------------------------------- + +interface ErrorCase { + name: string; + status: number; + /** The answer's body, exactly as sent. */ + body: string; + /** The API's text before any hint the SDK adds (TypeScript's `apiMessage`). */ + message: string; + /** The API's error code as this SDK reads it (`WebhooksCCError.code`), or null. */ + code: string | null; + /** Whether the SDK adds its recovery hint (401 and 404 under 30 characters). */ + hinted: boolean; +} + +const LONG_CODE = "a".repeat(1200); + +const ERROR_CASES: ErrorCase[] = [ + // A bare code says the status in words, alone when the code only names it. + { + name: "404 not_found", + status: 404, + body: '{"error":"not_found"}', + message: "Not found", + code: "not_found", + hinted: true, + }, + { + name: "401 unauthorized", + status: 401, + body: '{"error":"unauthorized"}', + message: "Unauthorized", + code: "unauthorized", + hinted: true, + }, + { + name: "403 forbidden", + status: 403, + body: '{"error":"forbidden"}', + message: "Forbidden", + code: "forbidden", + hinted: false, + }, + { + name: "409 conflict", + status: 409, + body: '{"error":"conflict"}', + message: "Conflict", + code: "conflict", + hinted: false, + }, + { + name: "400 bad_request", + status: 400, + body: '{"error":"bad_request"}', + message: "Bad request", + code: "bad_request", + hinted: false, + }, + { + name: "429 too_many_requests", + status: 429, + body: '{"error":"too_many_requests"}', + message: "Too many requests", + code: "too_many_requests", + hinted: false, + }, + { + name: "500 server_error", + status: 500, + body: '{"error":"server_error"}', + message: "Server error", + code: "server_error", + hinted: false, + }, + { + name: "410 names its own status words", + status: 410, + body: '{"error":"request_failed_with_http_410"}', + message: "Request failed with HTTP 410", + code: "request_failed_with_http_410", + hinted: false, + }, + // A code that says more than the status stays, in brackets. + { + name: "400 invalid_since", + status: 400, + body: '{"error":"invalid_since"}', + message: "Bad request (invalid_since)", + code: "invalid_since", + hinted: false, + }, + { + name: "400 invalid_cursor", + status: 400, + body: '{"error":"invalid_cursor"}', + message: "Bad request (invalid_cursor)", + code: "invalid_cursor", + hinted: false, + }, + { + name: "409 slug_exists", + status: 409, + body: '{"error":"slug_exists"}', + message: "Conflict (slug_exists)", + code: "slug_exists", + hinted: false, + }, + { + name: "401 invalid_token", + status: 401, + body: '{"error":"invalid_token"}', + message: "Unauthorized (invalid_token)", + code: "invalid_token", + hinted: true, + }, + { + name: "401 invalid_api_key is 30 characters, no hint", + status: 401, + body: '{"error":"invalid_api_key"}', + message: "Unauthorized (invalid_api_key)", + code: "invalid_api_key", + hinted: false, + }, + { + name: "404 a code with digits", + status: 404, + body: '{"error":"e404"}', + message: "Not found (e404)", + code: "e404", + hinted: true, + }, + { + name: "400 a code with digits and an underscore", + status: 400, + body: '{"error":"v2_bad"}', + message: "Bad request (v2_bad)", + code: "v2_bad", + hinted: false, + }, + { + name: "429 search_busy", + status: 429, + body: '{"error":"search_busy"}', + message: "Too many requests (search_busy)", + code: "search_busy", + hinted: false, + }, + { + name: "500 internal_error", + status: 500, + body: '{"error":"internal_error"}', + message: "Server error (internal_error)", + code: "internal_error", + hinted: false, + }, + { + name: "503 unavailable", + status: 503, + body: '{"error":"unavailable"}', + message: "Server error (unavailable)", + code: "unavailable", + hinted: false, + }, + { + name: "410 expired", + status: 410, + body: '{"error":"expired"}', + message: "Request failed with HTTP 410 (expired)", + code: "expired", + hinted: false, + }, + { + name: "418 teapot", + status: 418, + body: '{"error":"im_a_teapot"}', + message: "Request failed with HTTP 418 (im_a_teapot)", + code: "im_a_teapot", + hinted: false, + }, + { + name: "410 a message that is only a code", + status: 410, + body: '{"message":"expired"}', + message: "Request failed with HTTP 410 (expired)", + code: null, + hinted: false, + }, + { + name: "409 a message that is only a code, beside another code", + status: 409, + body: '{"error":"invalid_input","message":"name_taken"}', + message: "Conflict (name_taken)", + code: "invalid_input", + hinted: false, + }, + { + name: "400 a 1000-character code is whole", + status: 400, + body: JSON.stringify({ error: "a".repeat(1000) }), + message: `Bad request (${"a".repeat(1000)})`, + code: "a".repeat(1000), + hinted: false, + }, + { + name: "400 a 1200-character code is cut and kept as text", + status: 400, + body: JSON.stringify({ error: LONG_CODE }), + message: `${"a".repeat(1000)}...`, + code: LONG_CODE, + hinted: false, + }, + // Text that is not a code is kept as it is. + { + name: "404 a bare number is no code", + status: 404, + body: '{"error":"404"}', + message: "404", + code: null, + hinted: true, + }, + { + name: "404 a hyphen is no code", + status: 404, + body: '{"error":"not-found"}', + message: "not-found", + code: null, + hinted: true, + }, + { + name: "404 a capital letter is no code", + status: 404, + body: '{"error":"Not_found"}', + message: "Not_found", + code: null, + hinted: true, + }, + { + name: "404 a letter past ASCII is no code", + status: 404, + body: '{"error":"nöt_found"}', + message: "nöt_found", + code: null, + hinted: true, + }, + { + name: "404 a padded code is no code", + status: 404, + body: '{"error":" not_found "}', + message: " not_found ", + code: null, + hinted: true, + }, + { + name: "404 a code with a line break after it is no code", + status: 404, + body: '{"error":"not_found\\n"}', + message: "not_found\n", + code: null, + hinted: true, + }, + { + name: "404 a sentence", + status: 404, + body: '{"error":"Request not found"}', + message: "Request not found", + code: null, + hinted: true, + }, + { + name: "429 a sentence", + status: 429, + body: '{"error":"Rate limit exceeded"}', + message: "Rate limit exceeded", + code: null, + hinted: false, + }, + { + name: "503 a sentence", + status: 503, + body: '{"error":"Service unavailable"}', + message: "Service unavailable", + code: null, + hinted: false, + }, + // message wins over error; a blank or non-string message gives way. + { + name: "404 message beside a code", + status: 404, + body: '{"error":"not_found","message":"Request not found"}', + message: "Request not found", + code: "not_found", + hinted: true, + }, + { + name: "400 message beside invalid_cursor", + status: 400, + body: '{"error":"invalid_cursor","message":"The cursor is not one this search returned"}', + message: "The cursor is not one this search returned", + code: "invalid_cursor", + hinted: false, + }, + { + name: "400 message before invalid_cursor", + status: 400, + body: '{"message":"The cursor is not one this search returned","error":"invalid_cursor"}', + message: "The cursor is not one this search returned", + code: null, + hinted: false, + }, + { + name: "409 message beside a sentence", + status: 409, + body: '{"message":"Name is taken","error":"Conflict"}', + message: "Name is taken", + code: null, + hinted: false, + }, + { + name: "400 a blank message gives way", + status: 400, + body: '{"message":" ","error":"name is too long"}', + message: "name is too long", + code: null, + hinted: false, + }, + { + name: "400 a message only trim() calls blank (U+FEFF) gives way", + status: 400, + body: '{"message":"\\ufeff ","error":"name is too long"}', + message: "name is too long", + code: null, + hinted: false, + }, + { + name: "404 a message that is not a string gives way", + status: 404, + body: '{"message":5,"error":"Endpoint not found"}', + message: "Endpoint not found", + code: null, + hinted: true, + }, + { + name: "400 a message over 200 characters is read whole", + status: 400, + body: JSON.stringify({ error: "invalid_input", message: "y ".repeat(125) }), + message: "y ".repeat(125), + code: "invalid_input", + hinted: false, + }, + { + name: "400 a message over 1000 characters is cut", + status: 400, + body: JSON.stringify({ message: "m".repeat(1500) }), + message: `${"m".repeat(1000)}...`, + code: null, + hinted: false, + }, + { + name: "400 a cut never splits a surrogate pair", + status: 400, + body: JSON.stringify({ + error: "invalid_input", + message: "a".repeat(999) + "\u{1F600}".repeat(5), + }), + message: "a".repeat(999) + "\u{1F600}...", + code: "invalid_input", + hinted: false, + }, + // A code field beside the error. + { + name: "502 a code field beside a sentence", + status: 502, + body: '{"error":"Polar did not answer","code":"outcome_unknown"}', + message: "Polar did not answer", + code: "outcome_unknown", + hinted: false, + }, + { + name: "429 search_busy as a code field", + status: 429, + body: '{"error":"Search busy","code":"search_busy"}', + message: "Search busy", + code: "search_busy", + hinted: false, + }, + { + name: "400 a code field that is no code", + status: 400, + body: '{"error":"Bad things","code":"Not-A-Code"}', + message: "Bad things", + code: null, + hinted: false, + }, + // Bodies that are not a JSON object with text are kept, cut at 200 characters. + { + name: "502 plain text", + status: 502, + body: "Bad gateway", + message: "Bad gateway", + code: null, + hinted: false, + }, + { + name: "404 plain text that looks like a code", + status: 404, + body: "not_found", + message: "not_found", + code: null, + hinted: true, + }, + { + name: "404 a JSON string", + status: 404, + body: '"not_found"', + message: '"not_found"', + code: null, + hinted: true, + }, + { + name: "404 a JSON array", + status: 404, + body: '["not_found"]', + message: '["not_found"]', + code: null, + hinted: true, + }, + { name: "404 JSON null", status: 404, body: "null", message: "null", code: null, hinted: true }, + { + name: "400 an error that is an object", + status: 400, + body: '{"error":{"code":"x"}}', + message: '{"error":{"code":"x"}}', + code: null, + hinted: false, + }, + { + name: "400 empty message and error", + status: 400, + body: '{"message":"","error":""}', + message: '{"message":"","error":""}', + code: null, + hinted: false, + }, + { + name: "400 an object without either", + status: 400, + body: '{"detail":"x"}', + message: '{"detail":"x"}', + code: null, + hinted: false, + }, + { + name: "503 HTML", + status: 503, + body: "Service Unavailable", + message: "Service Unavailable", + code: null, + hinted: false, + }, + { name: "404 an empty body", status: 404, body: "", message: "", code: null, hinted: true }, + { + name: "500 a 200-character body is whole", + status: 500, + body: "z".repeat(200), + message: "z".repeat(200), + code: null, + hinted: false, + }, + { + name: "500 a long body is cut", + status: 500, + body: "z".repeat(400), + message: `${"z".repeat(200)}...`, + code: null, + hinted: false, + }, + { + name: "502 a cut body never splits a surrogate pair", + status: 502, + body: "b".repeat(199) + "\u{1F600}".repeat(50), + message: "b".repeat(199) + "\u{1F600}...", + code: null, + hinted: false, + }, + { + name: "400 unterminated JSON is plain text", + status: 400, + body: `{"error":"${"x".repeat(300)}`, + message: `{"error":"${"x".repeat(190)}...`, + code: null, + hinted: false, + }, + // The hint: 401 and 404 under 30 characters (UTF-16 units here). + { + name: "404 29 characters get a hint", + status: 404, + body: '{"error":"This endpoint does not exist."}', + message: "This endpoint does not exist.", + code: null, + hinted: true, + }, + { + name: "404 30 characters get none", + status: 404, + body: '{"error":"This endpoint does not exist!!"}', + message: "This endpoint does not exist!!", + code: null, + hinted: false, + }, + { + name: "401 a short sentence", + status: 401, + body: '{"error":"Invalid API key"}', + message: "Invalid API key", + code: null, + hinted: true, + }, + { + name: "401 a long sentence", + status: 401, + body: '{"error":"This API key was revoked by its owner."}', + message: "This API key was revoked by its owner.", + code: null, + hinted: false, + }, + { + name: "404 20 accented letters", + status: 404, + body: JSON.stringify({ error: "é".repeat(20) }), + message: "é".repeat(20), + code: null, + hinted: true, + }, + { + name: "404 20 emoji", + status: 404, + body: JSON.stringify({ error: "\u{1F600}".repeat(20) }), + message: "\u{1F600}".repeat(20), + code: null, + hinted: false, + }, +]; + +/** The error a call fails with when the API answers `status` with `body`. */ +async function errorFor(status: number, body: string): Promise { + const client = new WebhooksCC({ + apiKey: "whcc_conformance", + baseUrl: "https://webhooks.test", + fetch: (async () => + new Response(body, { + status, + headers: { "content-type": "application/json" }, + })) as typeof globalThis.fetch, + }); + try { + await client.endpoints.get("acme"); + } catch (error) { + expect(error).toBeInstanceOf(WebhooksCCError); + return error as WebhooksCCError; + } + throw new Error("expected the call to fail"); +} + +function buildErrorVectors() { + return ERROR_CASES.map(({ name, status, body, message, code, hinted }) => ({ + name, + status, + body, + expected: { message, code, hinted }, + })); +} + // --------------------------------------------------------------------------- afterEach(() => { @@ -1620,4 +2196,29 @@ describe("conformance vectors", () => { ) ).toMatchFileSnapshot("../../conformance/durations.json"); }); + + it("errors.json", async () => { + const names = new Set(); + for (const testCase of ERROR_CASES) { + expect(names.has(testCase.name), testCase.name).toBe(false); + names.add(testCase.name); + const error = await errorFor(testCase.status, testCase.body); + expect(error.statusCode, testCase.name).toBe(testCase.status); + expect(error.apiMessage, testCase.name).toBe(testCase.message); + expect(error.code ?? null, testCase.name).toBe(testCase.code); + const hintable = testCase.status === 401 || testCase.status === 404; + expect(hintable && error.message !== error.apiMessage, testCase.name).toBe(testCase.hinted); + if (testCase.hinted) { + expect(error.message.startsWith(testCase.message), testCase.name).toBe(true); + } else if (testCase.status !== 429) { + expect(error.message, testCase.name).toBe(testCase.message); + } + } + await expect( + file( + "Error messages made from API answers. A call (GET /api/endpoints/acme) answered with status and body (exactly these characters, as UTF-8) fails with an error whose text before any hint the SDK adds is expected.message: the body's string message, else its string error (a blank one, as JavaScript's trim() sees it, gives way), cut to 1000 characters with ... after it; a message that is only a code (lower-case ASCII letters, digits and underscores, starting with a letter) becomes the status in words, alone when the code only names the status, else with the code in brackets; a body that is not a JSON object with either is kept as it came, cut to 200 characters. Characters are code points, so a cut never splits a surrogate pair. expected.hinted says whether the SDK adds its recovery hint, which only a 401 or 404 message under 30 characters (UTF-16 units) gets; the hint's wording is each SDK's own. On any status but 401, 404 and 429 (whose message is each SDK's rate-limit text) the error's message is expected.message itself. expected.code is the API's error code as the TypeScript SDK reads it: error when it is the body's first key and a code, else a code field that is a code, else null. A port lists in its test the vectors where it differs, and why.", + buildErrorVectors() + ) + ).toMatchFileSnapshot("../../conformance/errors.json"); + }); }); From 962e9cb8090a8f015d3e35ab52babd1b2eb193cf Mon Sep 17 00:00:00 2001 From: Sauer Date: Mon, 16 Nov 2026 02:48:26 +0100 Subject: [PATCH 2/3] fix(sdk): read error codes and count hint length as TypeScript does, sdk 1.31.2, python and go 0.10.2 Review round 1 on #730. - TypeScript reads `error` as the code wherever it sits in the JSON object, then a `code` field; the leading-key regex is kept only for a body that does not parse. - Python's `_error_code` (behind RateLimitError.code and InvalidCursorError) and Go's unexported code take `error` only when it is a code, never a sentence. - Python and Go count the 30-character hint threshold in UTF-16 units, as TypeScript does (Python with surrogatepass, so a lone surrogate counts as one unit). - The Go conformance test goes through the public client; null-code vectors now assert that no code was read; the divergence lists keep only the `code` field, which Python and Go do not read. - New vectors: a 429 sentence that equals the status words, and a lone surrogate. Co-Authored-By: Claude Opus 5.5 --- .../web/error-codes-wherever-they-sit.json | 7 ++ apps/web/lib/changelog.ts | 35 +++++++++- packages/sdk-go/client.go | 12 ++-- packages/sdk-go/doc.go | 2 +- packages/sdk-go/errors_conformance_test.go | 70 ++++++++++--------- packages/sdk-python/pyproject.toml | 2 +- .../sdk-python/src/webhooks_cc/__init__.py | 2 +- packages/sdk-python/src/webhooks_cc/client.py | 8 ++- packages/sdk-python/tests/test_client.py | 3 +- .../tests/test_errors_conformance.py | 37 +++------- packages/sdk-python/uv.lock | 2 +- packages/sdk/conformance/errors.json | 24 ++++++- packages/sdk/package.json | 2 +- .../sdk/src/__tests__/conformance.test.ts | 20 +++++- packages/sdk/src/client.ts | 20 +++--- 15 files changed, 156 insertions(+), 90 deletions(-) create mode 100644 apps/web/changelog/web/error-codes-wherever-they-sit.json diff --git a/apps/web/changelog/web/error-codes-wherever-they-sit.json b/apps/web/changelog/web/error-codes-wherever-they-sit.json new file mode 100644 index 000000000..d666c124f --- /dev/null +++ b/apps/web/changelog/web/error-codes-wherever-they-sit.json @@ -0,0 +1,7 @@ +{ + "title": "Error codes read wherever they sit", + "items": [ + "The hosted MCP server, through SDK 1.31.2, reads an API error's code from its `error` wherever it sits in the JSON body, so a cursor or busy-search hint still shows when `message` comes first", + "The changelog lists SDK 1.31.2 and the Python and Go SDKs 0.10.2" + ] +} diff --git a/apps/web/lib/changelog.ts b/apps/web/lib/changelog.ts index bfd6718db..d4f21ccec 100644 --- a/apps/web/lib/changelog.ts +++ b/apps/web/lib/changelog.ts @@ -31,9 +31,9 @@ const RELEASE = release as { version: string | null; entries: ChangelogEntry[] } export const APP_VERSION: string = RELEASE.version ?? "dev"; export const CLI_VERSION = "1.20.1"; -export const SDK_VERSION = "1.31.1"; -export const PYTHON_SDK_VERSION = "0.10.1"; -export const GO_SDK_VERSION = "0.10.1"; +export const SDK_VERSION = "1.31.2"; +export const PYTHON_SDK_VERSION = "0.10.2"; +export const GO_SDK_VERSION = "0.10.2"; export const MCP_VERSION = "2.5.0"; export const CHANGELOG: ChangelogEntry[] = [ @@ -2546,6 +2546,15 @@ export const CHANGELOG: ChangelogEntry[] = [ }, // ─── SDK ──────────────────────────────────────────────────────────── + { + version: "1.31.2", + date: "2026-11-16", + title: "Error Codes Read Wherever They Sit", + track: "sdk", + items: [ + 'An error\'s `code` is read from the body\'s `error` wherever it sits in the JSON object, not only when it comes first, so `{ "message": "...", "error": "invalid_cursor" }` still has the code `invalid_cursor`', + ], + }, { version: "1.31.1", date: "2026-11-16", @@ -3042,6 +3051,16 @@ export const CHANGELOG: ChangelogEntry[] = [ }, // ─── Python SDK ───────────────────────────────────────────────────── + { + version: "0.10.2", + date: "2026-11-16", + title: "Error Codes And Hints Match The TypeScript SDK", + track: "python", + items: [ + '`RateLimitError.code` (and the code behind `InvalidCursorError`) is only ever a code such as `search_busy`, never a sentence: a 429 answering `{"error": "Too many requests"}` has no code', + "A short 401 or 404 message gets its recovery hint below 30 UTF-16 units, as in the TypeScript SDK, so messages with emoji get the same hint in both", + ], + }, { version: "0.10.1", date: "2026-11-16", @@ -3195,6 +3214,16 @@ export const CHANGELOG: ChangelogEntry[] = [ }, // ─── Go SDK ───────────────────────────────────────────────────────── + { + version: "0.10.2", + date: "2026-11-16", + title: "Error Codes And Hints Match The TypeScript SDK", + track: "go", + items: [ + "The code behind `errors.Is(err, ErrInvalidCursor)` and a busy search is only ever a code such as `invalid_cursor`, never a sentence", + "A short 401 or 404 message gets its recovery hint below 30 UTF-16 units, as in the TypeScript SDK, rather than 30 bytes, so a short message with accented letters gets its hint", + ], + }, { version: "0.10.1", date: "2026-11-16", diff --git a/packages/sdk-go/client.go b/packages/sdk-go/client.go index e055f968e..8c74e6e88 100644 --- a/packages/sdk-go/client.go +++ b/packages/sdk-go/client.go @@ -16,6 +16,7 @@ import ( "strconv" "strings" "time" + "unicode/utf16" "unicode/utf8" "webhooks.cc/go/agent" @@ -564,10 +565,7 @@ func errorText(body []byte) (string, bool) { // withHint adds a recovery hint to a short message: "Not found: Use // Endpoints.List ...", with the separators the TypeScript and Python SDKs // use. Unlike them, Go trims the message (as JavaScript's trim() does) -// before it adds the hint, and errorFromResponse calls a message short -// when it is under 30 bytes, where they count characters (UTF-16 units in -// TypeScript, code points in Python), so a short message with non-ASCII -// text can get no hint here and one there. +// before it adds the hint. func withHint(message, separator, hint string) string { message = webtext.JSTrim(message) if message == "" { @@ -595,12 +593,14 @@ func plainErrorText(status int, body []byte) string { func errorFromResponse(response *apiResponse, path string) *Error { message := plainErrorText(response.status, response.body) - generic := len(message) < 30 + // Short in UTF-16 units, as the TypeScript SDK counts them. + generic := len(utf16.Encode([]rune(message))) < 30 apiErr := &Error{StatusCode: response.status, Message: message} var coded struct { Error string `json:"error"` } - if json.Unmarshal(response.body, &coded) == nil { + // Only a code (invalid_cursor), never a sentence. + if json.Unmarshal(response.body, &coded) == nil && bareCode.MatchString(coded.Error) { apiErr.code = coded.Error } switch response.status { diff --git a/packages/sdk-go/doc.go b/packages/sdk-go/doc.go index a4a834867..431ae932f 100644 --- a/packages/sdk-go/doc.go +++ b/packages/sdk-go/doc.go @@ -39,4 +39,4 @@ package webhookscc // Version is the SDK's version. -const Version = "0.10.1" +const Version = "0.10.2" diff --git a/packages/sdk-go/errors_conformance_test.go b/packages/sdk-go/errors_conformance_test.go index f82fbb053..c1df9da0f 100644 --- a/packages/sdk-go/errors_conformance_test.go +++ b/packages/sdk-go/errors_conformance_test.go @@ -1,8 +1,10 @@ package webhookscc import ( + "context" "errors" "net/http" + "net/http/httptest" "strings" "testing" @@ -10,21 +12,16 @@ import ( "webhooks.cc/go/internal/webtext" ) -// Where this SDK differs from the TypeScript one on purpose, by vector name. -var ( - // Go counts bytes for the 30-character hint threshold, TypeScript UTF-16 - // units, so a short message with non-ASCII text can get no hint here. - errorHintDiffers = map[string]string{ - "404 20 accented letters": "20 letters are 40 bytes, over 30 here; TypeScript counts 20 UTF-16 units", - } - // Go reads a code only from "error", wherever it is in the body; - // TypeScript only from a leading "error" that is a code, or a "code" field. - errorCodeDiffers = map[string]string{ - "502 a code field beside a sentence": "the code field is not read", - "429 search_busy as a code field": "the code field is not read", - "400 message before invalid_cursor": "error is read wherever it is in the body", - } -) +// Where this SDK differs from the TypeScript one, and why, by vector name. +// Go reads a code only from "error", never from a "code" field. +var errorCodeDiffers = map[string]string{ + "502 a code field beside a sentence": "the code field is not read", + "429 search_busy as a code field": "the code field is not read", +} + +// Not compared at all: a 429's message is this SDK's own rate-limit text. +// Go trims a message, as JavaScript's trim() does, before its hint, so a +// hinted message starts with the trimmed text. func TestErrorsConformance(t *testing.T) { var file struct { @@ -57,37 +54,46 @@ func TestErrorsConformance(t *testing.T) { t.Fatalf("message %q, want %q", plain, want.Message) } - apiErr := errorFromResponse(&apiResponse{status: status, header: http.Header{}, body: body}, "/endpoints/acme") + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/endpoints/acme" { + t.Errorf("path %s", r.URL.Path) + } + w.WriteHeader(status) + _, _ = w.Write(body) + })) + defer server.Close() + client, err := New(WithAPIKey("whcc_conformance"), WithBaseURL(server.URL+"/")) + if err != nil { + t.Fatal(err) + } + _, err = client.Endpoints.Get(context.Background(), "acme") + var apiErr *Error + if !errors.As(err, &apiErr) { + t.Fatalf("error %v", err) + } if apiErr.StatusCode != status { t.Errorf("status %d", apiErr.StatusCode) } + code := "" if want.Code != nil { code = *want.Code } - codeAgrees := (want.Code == nil || apiErr.code == code) && - errors.Is(apiErr, ErrInvalidCursor) == (status == http.StatusBadRequest && code == "invalid_cursor") && - isBusySearch(apiErr) == (status == http.StatusTooManyRequests && code == "search_busy") + codeAgrees := apiErr.code == code && + errors.Is(apiErr, ErrInvalidCursor) == (status == http.StatusBadRequest && apiErr.code == "invalid_cursor") && + isBusySearch(apiErr) == (status == http.StatusTooManyRequests && apiErr.code == "search_busy") // A listed difference that no longer differs fails too, so the list stays true. if _, differs := errorCodeDiffers[vector.Name]; codeAgrees == differs { - t.Errorf("code %q agrees %v, listed as a difference %v", apiErr.code, codeAgrees, differs) + t.Errorf("code %q, want %q, listed as a difference %v", apiErr.code, code, differs) } - // A 429's message is this SDK's own rate-limit text. if status == http.StatusTooManyRequests { return } hinted := (status == http.StatusUnauthorized || status == http.StatusNotFound) && apiErr.Message != plain - if _, differs := errorHintDiffers[vector.Name]; differs { - if hinted == want.Hinted { - t.Errorf("hinted %v, listed as a difference", hinted) - } - return - } if hinted != want.Hinted { t.Fatalf("hinted %v, want %v (message %q)", hinted, want.Hinted, apiErr.Message) } - // Go trims the message, as JavaScript's trim() does, before its hint. if hinted && !strings.HasPrefix(apiErr.Message, webtext.JSTrim(plain)) { t.Errorf("message %q does not start with %q", apiErr.Message, plain) } @@ -96,11 +102,9 @@ func TestErrorsConformance(t *testing.T) { } }) } - for _, list := range []map[string]string{errorHintDiffers, errorCodeDiffers} { - for name := range list { - if !names[name] { - t.Errorf("no vector named %q", name) - } + for name := range errorCodeDiffers { + if !names[name] { + t.Errorf("no vector named %q", name) } } } diff --git a/packages/sdk-python/pyproject.toml b/packages/sdk-python/pyproject.toml index 57ed770da..ce8a9ee90 100644 --- a/packages/sdk-python/pyproject.toml +++ b/packages/sdk-python/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "webhooks-cc" -version = "0.10.1" +version = "0.10.2" description = "Python SDK for webhooks.cc: capture, wait for, sign and verify webhooks and test emails" readme = "README.md" license = "MIT" diff --git a/packages/sdk-python/src/webhooks_cc/__init__.py b/packages/sdk-python/src/webhooks_cc/__init__.py index 815acc381..bc0a14f73 100644 --- a/packages/sdk-python/src/webhooks_cc/__init__.py +++ b/packages/sdk-python/src/webhooks_cc/__init__.py @@ -20,7 +20,7 @@ Every signature check for each provider is in :mod:`webhooks_cc.verify`. """ -__version__ = "0.10.1" +__version__ = "0.10.2" from .agent import AgentAuthError, ClaimAttempt from .client import ( diff --git a/packages/sdk-python/src/webhooks_cc/client.py b/packages/sdk-python/src/webhooks_cc/client.py index d7bc5c96c..df94fe0b1 100644 --- a/packages/sdk-python/src/webhooks_cc/client.py +++ b/packages/sdk-python/src/webhooks_cc/client.py @@ -251,7 +251,8 @@ def _map_error( ) -> WebhooksCCError: """``message`` is the plain text; ``body`` the whole answer, to find its code in.""" raw = message if body is None else body - generic = len(message) < 30 + # Short in UTF-16 units, as the TypeScript SDK counts them. + generic = len(message.encode("utf-16-le", "surrogatepass")) // 2 < 30 if status == 401: return UnauthorizedError( f"{message}. Get an API key at https://webhooks.cc/account/api-keys" @@ -278,13 +279,14 @@ def _map_error( def _error_code(message: str) -> str | None: - """The ``error`` of a JSON error body, when it is one.""" + """The ``error`` of a JSON error body, when it is a code (``invalid_cursor``), + not a sentence.""" try: parsed = json.loads(message) except ValueError: return None code = parsed.get("error") if isinstance(parsed, dict) else None - return code if isinstance(code, str) else None + return code if isinstance(code, str) and _BARE_CODE.fullmatch(code) else None # Longest text taken from a JSON error body, so one answer cannot make a huge message. diff --git a/packages/sdk-python/tests/test_client.py b/packages/sdk-python/tests/test_client.py index 769508fa2..e4f2a0f35 100644 --- a/packages/sdk-python/tests/test_client.py +++ b/packages/sdk-python/tests/test_client.py @@ -2295,7 +2295,8 @@ def handler(request: httpx.Request) -> httpx.Response: client = make_client(handler) with pytest.raises(errors.RateLimitError) as caught: client.requests.search(q="x") - assert caught.value.code == "Too many requests" + # A sentence is no code: only search_busy and the like are. + assert caught.value.code is None with pytest.raises(errors.RateLimitError): client.requests.count(q="x") assert (len(calls), slept) == (2, []) diff --git a/packages/sdk-python/tests/test_errors_conformance.py b/packages/sdk-python/tests/test_errors_conformance.py index 5c21c1e22..21390104d 100644 --- a/packages/sdk-python/tests/test_errors_conformance.py +++ b/packages/sdk-python/tests/test_errors_conformance.py @@ -13,23 +13,14 @@ VECTORS = load("errors")["vectors"] -# Where this SDK differs from the TypeScript one on purpose, by vector name. - -# Python counts code points for the 30-character hint threshold, TypeScript -# UTF-16 units, so 20 emoji (40 units) are short here and get a hint. -HINT_DIFFERS = { - "404 20 emoji": "20 code points are under 30 here; TypeScript counts 40 UTF-16 units", -} - -# Python reads a code only from ``error``, wherever it is in the body and -# whatever it holds; TypeScript only from a leading ``error`` that is a code, -# or a ``code`` field. +# Where this SDK differs from the TypeScript one, and why, by vector name. +# Python reads a code only from ``error``, never from a ``code`` field. CODE_DIFFERS = { "502 a code field beside a sentence": "the code field is not read", "429 search_busy as a code field": "the code field is not read", - "400 message before invalid_cursor": "error is read wherever it is in the body", - "429 a sentence": "RateLimitError.code is the error as sent, a sentence included", } +# Not compared at all: a 429's message is this SDK's own rate-limit text, and +# InvalidCursorError's message is its own. def _raise(vector: dict[str, Any]) -> errors.WebhooksCCError: @@ -54,22 +45,18 @@ def test_error_vector(vector: dict[str, Any]) -> None: error = _raise(vector) assert error.status_code == status cursor = isinstance(error, errors.InvalidCursorError) - name = vector["name"] + code = _error_code(body) code_agrees = ( - (expected["code"] is None or _error_code(body) == expected["code"]) - and cursor == (status == 400 and expected["code"] == "invalid_cursor") - and (not isinstance(error, errors.RateLimitError) or error.code == expected["code"]) + code == expected["code"] + and cursor == (status == 400 and code == "invalid_cursor") + and (not isinstance(error, errors.RateLimitError) or error.code == code) ) # A listed difference that no longer differs fails too, so the list stays true. - assert code_agrees == (name not in CODE_DIFFERS) + assert code_agrees == (vector["name"] not in CODE_DIFFERS) - # 429 and an invalid cursor have messages of their own. if status == 429 or cursor: return hinted = status in (401, 404) and error.message != plain - if name in HINT_DIFFERS: - assert hinted != expected["hinted"] - return assert hinted == expected["hinted"] if hinted: assert error.message.startswith(plain) @@ -77,7 +64,5 @@ def test_error_vector(vector: dict[str, Any]) -> None: assert error.message == plain -def test_the_lists_of_differences_name_real_vectors() -> None: - names = {vector["name"] for vector in VECTORS} - assert set(HINT_DIFFERS) <= names - assert set(CODE_DIFFERS) <= names +def test_the_list_of_differences_names_real_vectors() -> None: + assert set(CODE_DIFFERS) <= {vector["name"] for vector in VECTORS} diff --git a/packages/sdk-python/uv.lock b/packages/sdk-python/uv.lock index 02418261b..5a3d3c478 100644 --- a/packages/sdk-python/uv.lock +++ b/packages/sdk-python/uv.lock @@ -744,7 +744,7 @@ wheels = [ [[package]] name = "webhooks-cc" -version = "0.10.1" +version = "0.10.2" source = { editable = "." } dependencies = [ { name = "httpx" }, diff --git a/packages/sdk/conformance/errors.json b/packages/sdk/conformance/errors.json index aec5c09da..c41ed8d7b 100644 --- a/packages/sdk/conformance/errors.json +++ b/packages/sdk/conformance/errors.json @@ -1,7 +1,7 @@ { "version": 1, "generatedBy": "packages/sdk/src/__tests__/conformance.test.ts", - "description": "Error messages made from API answers. A call (GET /api/endpoints/acme) answered with status and body (exactly these characters, as UTF-8) fails with an error whose text before any hint the SDK adds is expected.message: the body's string message, else its string error (a blank one, as JavaScript's trim() sees it, gives way), cut to 1000 characters with ... after it; a message that is only a code (lower-case ASCII letters, digits and underscores, starting with a letter) becomes the status in words, alone when the code only names the status, else with the code in brackets; a body that is not a JSON object with either is kept as it came, cut to 200 characters. Characters are code points, so a cut never splits a surrogate pair. expected.hinted says whether the SDK adds its recovery hint, which only a 401 or 404 message under 30 characters (UTF-16 units) gets; the hint's wording is each SDK's own. On any status but 401, 404 and 429 (whose message is each SDK's rate-limit text) the error's message is expected.message itself. expected.code is the API's error code as the TypeScript SDK reads it: error when it is the body's first key and a code, else a code field that is a code, else null. A port lists in its test the vectors where it differs, and why.", + "description": "Error messages made from API answers. A call (GET /api/endpoints/acme) answered with status and body (exactly these characters, as UTF-8) fails with an error whose text before any hint the SDK adds is expected.message: the body's string message, else its string error (a blank one, as JavaScript's trim() sees it, gives way), cut to 1000 characters with ... after it; a message that is only a code (lower-case ASCII letters, digits and underscores, starting with a letter) becomes the status in words, alone when the code only names the status, else with the code in brackets; a body that is not a JSON object with either is kept as it came, cut to 200 characters. Characters are code points, so a cut never splits a surrogate pair. expected.hinted says whether the SDK adds its recovery hint, which only a 401 or 404 message under 30 characters (UTF-16 units) gets; the hint's wording is each SDK's own. On any status but 401, 404 and 429 (whose message is each SDK's rate-limit text) the error's message is expected.message itself. expected.code is the API's error code as the TypeScript SDK reads it: error when it is a code, wherever it sits in the object, else a code field that is a code, else null; a sentence is never a code. A port lists in its test the vectors where it differs, and why.", "vectors": [ { "name": "404 not_found", @@ -323,6 +323,16 @@ "hinted": false } }, + { + "name": "429 the status words as a sentence", + "status": 429, + "body": "{\"error\":\"Too many requests\"}", + "expected": { + "message": "Too many requests", + "code": null, + "hinted": false + } + }, { "name": "503 a sentence", "status": 503, @@ -359,7 +369,7 @@ "body": "{\"message\":\"The cursor is not one this search returned\",\"error\":\"invalid_cursor\"}", "expected": { "message": "The cursor is not one this search returned", - "code": null, + "code": "invalid_cursor", "hinted": false } }, @@ -653,6 +663,16 @@ "hinted": true } }, + { + "name": "404 a lone surrogate counts as one character", + "status": 404, + "body": "{\"error\":\"\\ud800\"}", + "expected": { + "message": "\ud800", + "code": null, + "hinted": true + } + }, { "name": "404 20 emoji", "status": 404, diff --git a/packages/sdk/package.json b/packages/sdk/package.json index 4b3a95b03..8880eb790 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -1,6 +1,6 @@ { "name": "@webhooks-cc/sdk", - "version": "1.31.1", + "version": "1.31.2", "description": "TypeScript SDK for webhooks.cc \u2014 create endpoints, capture requests, assert in tests", "main": "dist/index.js", "module": "dist/index.mjs", diff --git a/packages/sdk/src/__tests__/conformance.test.ts b/packages/sdk/src/__tests__/conformance.test.ts index 887d05deb..944fa668f 100644 --- a/packages/sdk/src/__tests__/conformance.test.ts +++ b/packages/sdk/src/__tests__/conformance.test.ts @@ -1805,6 +1805,14 @@ const ERROR_CASES: ErrorCase[] = [ code: null, hinted: false, }, + { + name: "429 the status words as a sentence", + status: 429, + body: '{"error":"Too many requests"}', + message: "Too many requests", + code: null, + hinted: false, + }, { name: "503 a sentence", status: 503, @@ -1835,7 +1843,7 @@ const ERROR_CASES: ErrorCase[] = [ status: 400, body: '{"message":"The cursor is not one this search returned","error":"invalid_cursor"}', message: "The cursor is not one this search returned", - code: null, + code: "invalid_cursor", hinted: false, }, { @@ -2062,6 +2070,14 @@ const ERROR_CASES: ErrorCase[] = [ code: null, hinted: true, }, + { + name: "404 a lone surrogate counts as one character", + status: 404, + body: '{"error":"\\ud800"}', + message: "\ud800", + code: null, + hinted: true, + }, { name: "404 20 emoji", status: 404, @@ -2216,7 +2232,7 @@ describe("conformance vectors", () => { } await expect( file( - "Error messages made from API answers. A call (GET /api/endpoints/acme) answered with status and body (exactly these characters, as UTF-8) fails with an error whose text before any hint the SDK adds is expected.message: the body's string message, else its string error (a blank one, as JavaScript's trim() sees it, gives way), cut to 1000 characters with ... after it; a message that is only a code (lower-case ASCII letters, digits and underscores, starting with a letter) becomes the status in words, alone when the code only names the status, else with the code in brackets; a body that is not a JSON object with either is kept as it came, cut to 200 characters. Characters are code points, so a cut never splits a surrogate pair. expected.hinted says whether the SDK adds its recovery hint, which only a 401 or 404 message under 30 characters (UTF-16 units) gets; the hint's wording is each SDK's own. On any status but 401, 404 and 429 (whose message is each SDK's rate-limit text) the error's message is expected.message itself. expected.code is the API's error code as the TypeScript SDK reads it: error when it is the body's first key and a code, else a code field that is a code, else null. A port lists in its test the vectors where it differs, and why.", + "Error messages made from API answers. A call (GET /api/endpoints/acme) answered with status and body (exactly these characters, as UTF-8) fails with an error whose text before any hint the SDK adds is expected.message: the body's string message, else its string error (a blank one, as JavaScript's trim() sees it, gives way), cut to 1000 characters with ... after it; a message that is only a code (lower-case ASCII letters, digits and underscores, starting with a letter) becomes the status in words, alone when the code only names the status, else with the code in brackets; a body that is not a JSON object with either is kept as it came, cut to 200 characters. Characters are code points, so a cut never splits a surrogate pair. expected.hinted says whether the SDK adds its recovery hint, which only a 401 or 404 message under 30 characters (UTF-16 units) gets; the hint's wording is each SDK's own. On any status but 401, 404 and 429 (whose message is each SDK's rate-limit text) the error's message is expected.message itself. expected.code is the API's error code as the TypeScript SDK reads it: error when it is a code, wherever it sits in the object, else a code field that is a code, else null; a sentence is never a code. A port lists in its test the vectors where it differs, and why.", buildErrorVectors() ) ).toMatchFileSnapshot("../../conformance/errors.json"); diff --git a/packages/sdk/src/client.ts b/packages/sdk/src/client.ts index 8ae27b886..c1da9f243 100644 --- a/packages/sdk/src/client.ts +++ b/packages/sdk/src/client.ts @@ -367,20 +367,22 @@ function cutText(text: string, max: number): string { /** * The API's error code in a JSON error body, when it names one: the error - * itself as a code (`{ "error": "invalid_cursor" }`), or a `code` beside the - * message (`{ "error": "...", "code": "outcome_unknown" }`). + * itself as a code (`{ "error": "invalid_cursor" }`, wherever it sits in the + * object), or a `code` beside the message (`{ "error": "...", "code": + * "outcome_unknown" }`). */ function errorCodeOf(body: string): string | undefined { - // Matched rather than parsed first: a body may arrive here cut short. - const bare = /^\{\s*"error"\s*:\s*"([a-z][a-z0-9_]*)"/.exec(body)?.[1]; - if (bare !== undefined) return bare; + let parsed: unknown; try { - const parsed = JSON.parse(body) as { code?: unknown } | null; - const code = parsed && typeof parsed === "object" ? parsed.code : undefined; - return typeof code === "string" && BARE_CODE.test(code) ? code : undefined; + parsed = JSON.parse(body); } catch { - return undefined; + // A body cut short is no JSON: a leading bare code is still read. + return /^\{\s*"error"\s*:\s*"([a-z][a-z0-9_]*)"/.exec(body)?.[1]; } + if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return undefined; + const { error, code } = parsed as { error?: unknown; code?: unknown }; + if (typeof error === "string" && BARE_CODE.test(error)) return error; + return typeof code === "string" && BARE_CODE.test(code) ? code : undefined; } function statusError( From 8679b68b89e145aa86b449e77c15e8d39156dfdc Mon Sep 17 00:00:00 2001 From: Sauer Date: Mon, 16 Nov 2026 02:55:07 +0100 Subject: [PATCH 3/3] chore(sdk): clearer comments and changelog wording for error codes Co-Authored-By: Claude Opus 5.5 --- apps/web/changelog/web/error-codes-wherever-they-sit.json | 2 +- packages/sdk-go/errors.go | 3 ++- packages/sdk/src/client.ts | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/apps/web/changelog/web/error-codes-wherever-they-sit.json b/apps/web/changelog/web/error-codes-wherever-they-sit.json index d666c124f..4a25000e5 100644 --- a/apps/web/changelog/web/error-codes-wherever-they-sit.json +++ b/apps/web/changelog/web/error-codes-wherever-they-sit.json @@ -1,7 +1,7 @@ { "title": "Error codes read wherever they sit", "items": [ - "The hosted MCP server, through SDK 1.31.2, reads an API error's code from its `error` wherever it sits in the JSON body, so a cursor or busy-search hint still shows when `message` comes first", + "The hosted MCP server, through SDK 1.31.2, reads an API error's code from its `error` wherever it sits in the JSON body, should an answer ever put `message` first, so its cursor and busy-search hints keep working", "The changelog lists SDK 1.31.2 and the Python and Go SDKs 0.10.2" ] } diff --git a/packages/sdk-go/errors.go b/packages/sdk-go/errors.go index f22a2c1be..47971611b 100644 --- a/packages/sdk-go/errors.go +++ b/packages/sdk-go/errors.go @@ -40,7 +40,8 @@ type Error struct { RetryAfter time.Duration // RateLimit is set on a 429 that carried all the X-RateLimit headers. RateLimit *RateLimit - // code is the "error" of a JSON error body ("search_busy", "invalid_cursor"). + // code is the "error" of a JSON error body when it is a code (lower case, + // digits, underscores: "search_busy", "invalid_cursor"), never a sentence. code string } diff --git a/packages/sdk/src/client.ts b/packages/sdk/src/client.ts index c1da9f243..cc4b8d555 100644 --- a/packages/sdk/src/client.ts +++ b/packages/sdk/src/client.ts @@ -376,7 +376,7 @@ function errorCodeOf(body: string): string | undefined { try { parsed = JSON.parse(body); } catch { - // A body cut short is no JSON: a leading bare code is still read. + // A body that is not valid JSON (a proxy cut it short): a leading bare code is still read. return /^\{\s*"error"\s*:\s*"([a-z][a-z0-9_]*)"/.exec(body)?.[1]; } if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return undefined;