API error architecture
The record is from 2026-10-21, updated on 2026-10-27.
This page records why jodapp-api error handling is changing.
- The parent effort is jod-app/roadmap#92, "2026 Q4 - API Error Architecture".
- jod-app/docs#171, "API Error Architecture - Initial", asks for this page.
- It lists every option we weighed, and the temporary ruling.
- It came out of the Billing interface design, Phase 1 of jod-app/roadmap#87.
- The code was read on
feature/backend-billing-phase-3ate545b6089and14baad523.
Words
| Word | Meaning |
|---|---|
| An entry point | A way into the code: a controller action, a Sidekiq job, a rake task or the console. |
The class Errors::BaseError | The parent of every error in app/shared/errors/, with title, messages, code, server, status and status_name. |
The class Errors::ValidationError | The 422 a manager raises from the errors of its validator. |
The class Errors::UnprocessableContentError | The 422 a manager raises for a business rule, per app/domains/AGENTS.md rule 3. |
| A manager | The class that owns one endpoint flow and its transaction, such as Ads::Campaigns::TeamSubmitManager. |
| A domain service | A class another domain calls, such as the planned Billing::SpendService. |
The class Billing::SpendValidator | The planned public Billing check that a product validator runs before the transaction and merges with errors.merge!. |
| The guard | The second credit check inside a write method, under lock('FOR UPDATE') on the balance row. |
| A business rejection | A failure where a rule said no, such as not enough credits. |
| An invariant violation | A failure that means the data is wrong, such as a ledger that disagrees with its balance row. |
Why we are looking at this now
Errors::BaseError was written when every entry point was a controller, and that is no longer true.
- Billing used to serve only its own Team Portal endpoints.
- Now
Billing::SpendServiceis called by product managers inside requests and by product jobs with no request.- Rake tasks and the console will call it too.
- One object carries the user words and the HTTP status.
- So a job or a console holds a status it can never send.
- Ali (CTO) requires that every Billing error reaches Sentry.
- Nothing in Billing sets
skip_sentry.
- Nothing in Billing sets
The model we agreed
Each failure has two owners: the domain says what went wrong, and the entry point reports it.
- The domain that knows what happened owns the error class, its words and its
code. - The entry point decides how to report the error.
- A controller action answers with JSON and an HTTP status.
- A Sidekiq job rolls back one record, reports it to Sentry, and moves to the next record.
- A rake task or the Rails console prints the error class and its message.
| Entry point | Who catches | What handling means there |
|---|---|---|
| Controller | ApplicationController rescue_from | JSON with a status, Sentry by policy |
| Sidekiq job | One rescue per record | Roll back the record, report with the record id, move on |
| Rake task or console | Nobody | Print the class and message, Sentry through the Rails integration |
Three kinds of failure, and only one gets a named class.
| Kind | Example in Billing | Who acts on it | Shape |
|---|---|---|---|
| A business rule said no | Not enough credits | A caller, by class | A named class under one base class per domain |
| An invariant broke | Ledger and balance disagree, consume with no reserve entry, a company with no billing account | Nobody, it is a bug | A plain error, never a Billing::Error |
| The write was called wrong | No open transaction, wrong argument type | Nobody, it is a bug | A plain error |
Boundary rule: only an entry point catches an error.
- A domain raises its own errors, and lets the errors of another domain pass through.
- A product manager never rescues a Billing error to translate it.
Errors::ValidationErrorstays the manager's raise, because a request failing validation is request-shaped by nature.
Every option we considered, so nobody weighs them again
For what Billing::SpendService raises when the balance is too low under the lock
| Shape | What it is | Verdict | Why |
|---|---|---|---|
| 1 | The shared Errors::UnprocessableContentError, no new class, code: 'insufficient_credits' added later if the web needs it | Ruled, then reopened the same day | What a public interface raises is part of its contract, and a job would rescue an HTTP-named error |
| 2 | A pure Billing::InsufficientCreditsError < StandardError, and every product manager rescues it and re-raises the shared 422 | Rejected | A rescue and re-raise in every manager that carries no decision |
| 2b | The pure error, and one translation at the HTTP edge in ApplicationController | Temporarily ruled, then generalised into option B below | The textbook shape, as Rails maps ActiveRecord::RecordNotFound to 404 in rescue_responses |
| 3 | Billing::InsufficientCreditsError < Errors::UnprocessableContentError with code: 'insufficient_credits', Imam's proposal plus the code | Codex chose it on run 1; it is the bridge below | Rides the existing renderer, but Billing inherits HTTP |
For the parent class of Billing::Error
| Option | What it is | Verdict | Why |
|---|---|---|---|
| A | Billing::Error < Errors::UnprocessableContentError, no shared-error change | The bridge until B lands | Billing names HTTP, and a job holds a status it cannot send |
| B | Errors::BaseError drops status and status_name, and ApplicationController holds one class-to-status table; Billing::Error < Errors::BaseError | Temporarily ruled 2026-10-21 | One hierarchy, no copy, HTTP only in the controller, every raise site unchanged |
| C | A new Errors::DomainError < StandardError beside Errors::BaseError, with the same words fields minus status | Rejected | Ali saw it is a copy of BaseError without the status, so two parallel hierarchies |
Other choices made on the way
| Choice | Ruling | Why |
|---|---|---|
| The pre-check | Billing::SpendValidator, run by the product validator with values, merged with errors.merge! | Billing owns the rule and the words; the dependency points one way |
validates_with Billing::SpendValidator, ... | Rejected | Billing would call methods on a product object by name, with no sig |
available_units as the public pre-check | Replaced by the validator | Every product repeated a rescue and the words |
| The error key | <instrument>_credits, such as placement_credits | One key for the validator and the guard, and the web branches on this key |
The wire code | insufficient_credits, set explicitly, never the Ruby class name | A Ruby rename must not break a web constant |
Billing::Error base class | Yes, one per domain | The entry point and a job catch "any Billing rejection" in one line; invariant violations never inherit it |
skip_sentry in Billing | Never | A guard failure is a race or a skipped validator, and both must be seen |
Default status for an unmapped Errors::BaseError under B | 500, not 422 | An unmapped error is a bug |
What Codex said
Three runs, gpt-6-astra, effort xhigh, read-only, on 2026-10-21.
- Run 1, shapes 1 to 3: chose Shape 3.
- It named the HTTP inheritance as a real coupling, accepted because the renderer exists.
- One class only, no base class.
- The web cannot see every shortage through
code, because the normal path returnsErrors::ValidationError.
- Run 2 gave no answer.
- Run 3, options A to C: chose B.
- Nothing outside
ApplicationController#base_errorreadserror.statusorstatus_name, inapp/,config/ortest/. - Ten files change:
BaseError,ApplicationController, eight subclasses,TooManyRequestsErrorat 429 included. - Remove the status readers' sigs too, make
requestnullable, drop or initializelevel, and pass a string tosuper. - Keep
messagesas a Hash of Symbol to Array of String on the domain error. - In
before_send, classify by class before theskip_sentryrule, and exempt everyBilling::Error. - Production
config/sentry.ymlhassample_rate: 0.5, and sampling runs beforebefore_send, so half of all errors never reach Sentry today.
- Nothing outside
The temporary ruling, 2026-10-21
Option B, with the plan below, pending one investigation.
- The investigation: whether B changes anything the frontend error framework reads.
- The JSON shape stays identical, so the expected answer is no, and it must be checked.
- Check
jodapp-webapp/api/ky-client.js,app/utils/error-utils.js,app/components/error-display/api-error-view.jsx, and everyerror.status,error.codeandstatus_nameread.
The plan
| PR | Where | What | Size |
|---|---|---|---|
| 1 | jodapp-api main | BaseError drops the status and its sigs, fixes request, level and super; eight subclasses drop @status_name; ApplicationController gets STATUS_BY_ERROR_CLASS and an ancestor lookup with 500 as default; one test per mapped class | Ten files, about a hundred lines |
| 2 | jodapp-api main | config/sentry.yml error sample_rate to 1.0 in production; before_send classifies by class before skip_sentry | Two files |
| 3 | jodapp-api main, after the Billing interface merges | Billing::Error inherits Errors::BaseError; one table line Billing::Error => :unprocessable_content | Two lines |
- One convention page in
docs/90-99-engineering-meta/96-conventions/, and four lines inapp/domains/AGENTS.mdrule 3. - The other four services that raise shared errors today move when someone next changes their file.
The bridge: what Imam builds now without waiting for B
The Billing interface builds Shape 3, in jodapp-api PR #2224, and B later changes one line.
app/domains/billing/error.rbwithBilling::Error < Errors::UnprocessableContentError, andapp/domains/billing/insufficient_credits_error.rbwithBilling::InsufficientCreditsError < Billing::Error.- It sets
code: 'insufficient_credits'explicitly, and Billing's words under<instrument>_credits. SpendServiceraises it under the lock, and no manager rescues it.- A job rescues
Billing::Errorfirst, thenStandardError. - When B lands, the parent line becomes
< Errors::BaseErrorand the table gets one line.- No raise site, rescue site, test or web constant changes.
Open items
- The frontend investigation above.
- Who builds PR 1 and PR 2: an Opus engineer agent from a brief, or Imam.
- Decided 2026-10-27: a company without an account is not an allowed state, so
Billing::NoBillingAccountErrordoes not exist.- A missing account raises
Errors::InternalServerError, which Sentry reports as an error. - Reason: every company is created with its own account, and jodapp-api PR #2183 created the account of every older company.
- A missing account raises
- The Billing page
overview/interface.mdand Billing D18 are on docs PR #165.- Since 2026-10-27, both name
Billing::ErrorandBilling::InsufficientCreditsErroras the current classes.
- Since 2026-10-27, both name