fix: tell export clients which terms the chosen format cannot hold [T-2599]

Merged
EV
6d ago
+181-15

What was wrong

Some projects cannot be exported in some formats: a project with the terms app and app.title in jsonnested, yamlnested or php, or a project with a term @x in arb. On develop (e0acd75):

  • api/src/formatters/nested-key-conflict.ts and api/src/formatters/arb.ts:45-47 build a message that names the terms, for example Nested JSON cannot hold the term "app" together with "app.title": ....
  • api/src/controllers/exports.controller.ts:163-164, :174-175, :189-190 and :200-201 wrap that message in a plain BadRequestException.
  • api/src/filters/exception.filter.ts:88-92 answers every 400 with the fixed message Your request seems to be invalid or malformed, so the terms are lost.
  • The webapp downloads the export with responseType: 'blob' (webapp/src/app/projects/services/export.service.ts:25). Its error body is a Blob that nobody reads (webapp/src/app/shared/util/api-error.ts:46), so the webapp shows a general hint without the term.

API clients got "invalid or malformed", and webapp users got a hint with no term name. In a large project, neither could tell which term to rename.

What changed

  • api/src/errors/index.ts: new FormatCannotHoldTermsException, a subclass of BadRequestException.
  • exports.controller.ts: the four catches (jsonnested, yamlnested, php, arb) now throw this exception instead of a plain BadRequestException. The 400 description in the Swagger decorator says that the message names the terms.
  • exception.filter.ts: answers this exception with status 400, code BadRequest and the exception's own message. The check comes right after AuthProviderException and before the database checks. Some of those checks match on the message text (UNIQUE constraint failed), and a quoted term name could contain that text. Every other 400 still gets the fixed message.
  • webapp/.../export.service.ts: when an export fails with 400 and the body is a Blob, the service parses it as JSON and rethrows the HttpErrorResponse with the parsed body. If the body cannot be parsed, the original error is rethrown unchanged.
  • webapp/.../api-error.ts: for a failed ExportLocale with status 400, the webapp shows the API's message when there is one. Otherwise it shows the same general hint as before.
  • docs/changelog.md: one line under next. docs/api/v1/errors.md: the 400 section shows the new message.

The response shape and status do not change: still 400 { error: { code: 'BadRequest', message } }. Only the message text is new.

Tests (written first, failing before the fix)

  • api/src/filters/exception.filter.spec.ts: three new tests. (1) The new exception keeps its message. (2) It keeps its message even when that message quotes UNIQUE constraint failed. (3) A plain BadRequestException still gets the fixed message.
    • On develop the suite does not compile (TS2305: Module '"../errors"' has no exported member 'FormatCannotHoldTermsException'). With only the exception class added and the filter unchanged: 2 failed, 3 passed (Received: "Your request seems to be invalid or malformed", and Expected: 400, Received: 409 for the quoted database error).
    • After: 5 of 5 passed.
  • api/test/export.e2e-spec.ts:308: for jsonnested, yamlnested and php, the test now also checks that the message contains cannot hold the term "button" together with "button.tooltip". A new test creates the term @x, exports in arb, and checks for 400 with rename these terms before exporting: @x.
    • Before (SQLite): 2 failed, 10 passed (Received string: "Your request seems to be invalid or malformed").
    • After: 12 of 12 passed on SQLite, Postgres 15 and MySQL 5.7.
  • webapp/.../export-locale.component.spec.ts: the component runs with the real ExportService against HttpTestingController. The export is answered with 400 and a Blob body. (1) A JSON body: the component shows the API's message. (2) A body that is not JSON: the component shows the general hint.
    • Before: 1 failed, 4 passed (the component showed the general hint, not the message).
    • After: 5 of 5 passed.

Checks run (Windows, Node 24)

  • yarn check-fmt at the root (prettier --check "**/*.ts"): all files use Prettier code style.
  • yarn --cwd api lint: clean.
  • tsc -p api/tsconfig.json --noEmit: clean.
  • api unit tests (yarn --cwd api test, --maxWorkers=2): 198 of 199 passed, 28 of 29 suites. The one failure is src/formatters/properties.spec.ts "should export properties files", which this PR does not touch. It fails only on Windows, because the properties library writes \r\n on win32. CircleCI and GitHub Actions run on Linux.
  • api e2e, full suite on SQLite (jest --config ./test/jest-e2e.json --runInBand --forceExit): 21 of 21 suites, 190 of 190 tests passed.
  • api e2e on Postgres 15 (throwaway container, same settings as ci.yml): export.e2e-spec.ts passed 12 of 12. I started the full suite on Postgres, but on this shared PC each suite took about ten times as long as on SQLite. I stopped it after 4 of 21 suites, all passed, and left the full Postgres and MySQL runs to the api-e2e CI matrix.
  • api e2e on MySQL 5.7 (throwaway container, data on tmpfs because the shared PC's disk made each migration run take minutes): export.e2e-spec.ts passed 12 of 12.
  • webapp unit tests (yarn test --watch=false --browsers=ChromeHeadlessCI --progress=false, as CI runs them): 65 of 65 passed.

Fleet task T-2599; authored by the lead lane while the fleet is paused by its daily cost ceiling.

šŸ¤– Generated with Claude Code