A Scraper's failures are part of its contract. The Definition's error schema lists every way a Run can fail, and the compiler rejects an Implementation that can fail any other way.
There are two kinds of declared errors: Shared errors, which every Scraper may use, and a Scraper's own errors.
Shared errors
Shared errors describe what went wrong in terms a caller can act on.
| Error | Means | A caller can |
|---|---|---|
Blocked | The source refused us: HTTP 401 or 403, a challenge page, an empty body. | Stop. Retrying the same way will be refused again. |
RateLimited | The source asked to slow down: HTTP 429. | Wait and retry. |
UpstreamDown | The source failed or could not be reached: 5xx, a timeout, a transport error. | Retry later. |
QuotaExhausted | A paid quota ran out, for example HTTP 402. | Top up or wait for the quota to reset. |
SchemaDrift | The source's data, or the Scraper's output, no longer matches its schema. | Repair the Scraper. Retrying will not help. |
InvalidInput | The input does not match the Definition. | Change the input. Retrying will not help. |
RunTimeout | The Run reached its Release's maximum duration on an instance (Run duration). The items before it are kept. | Narrow the input, or raise the Release's maxRunDuration. |
RequestLimitExceeded | The Run's requests reached Cloudflare's limit on outgoing requests from one Run on an instance, 10,000 by default (Known gaps). The items before it are kept. | Split the work into Runs of fewer requests. Retrying the same input will hit the limit again. |
All of them are Schema.TaggedError classes with a reason string. SchemaDrift also has a sample: up to 500 characters of the data that did not match.
new Blocked({ reason: `GET ${url}: HTTP 403` });
new SchemaDrift({ reason: issue.message, sample: body.slice(0, 500) });Who raises them
- Your Implementation raises
Blocked,RateLimited,UpstreamDown,QuotaExhaustedandSchemaDrift. The HTTP helpers do most of that mapping for you. Scraper.runraisesInvalidInputwhen the input fails the schema, andSchemaDriftwhen an item fails the output schema. You do not need to declare those two for that purpose: on an instance the result record'sfailurecarries them either way.- An instance raises
RunTimeoutwhen a Run reaches its maximum duration, andRequestLimitExceededwhen a Run fails after Cloudflare refused one of its requests for the subrequest limit, in place of theUpstreamDownthe Scraper saw. Declare them so callers decode them with the rest of your errors; the result record carries them either way.
Declare InvalidInput in your Definition only when the Implementation itself can reject an input the schema allows, for example a date the source has no data for.
Your own errors
When a failure is specific to one source, declare it as a Schema.TaggedError in the Scraper's package and add it to the Definition's union.
Example (A Scraper's own error, declared next to the Shared ones)
class SoldOut extends Schema.TaggedError<SoldOut>()("SoldOut", {
remaining: Schema.FiniteFromString,
}) {}
const Tickets = Scraper.make("tickets", {
error: Schema.Union([Blocked, RateLimited, SchemaDrift, UpstreamDown, SoldOut]),
input: Schema.Struct({ event: Schema.NonEmptyString }),
output: Schema.Struct({ price: Schema.FiniteFromString }),
});SoldOut is a fact about this source, so it carries what a caller needs: how many tickets remain. Give an error the fields that let the caller decide what to do next.
Nothing reclassifies a Scraper's own errors into Shared errors. A caller that runs the Scraper in process sees these tags unchanged.
Choosing an error
| The source answered | Use |
|---|---|
| 401, 403, a login wall, a bot challenge | Blocked |
| 429 | RateLimited |
| 402 or "limit reached" for a paid plan | QuotaExhausted |
| 5xx, timeout, connection refused | UpstreamDown |
| 200 with a body you cannot parse | SchemaDrift |
| 200 with a challenge page | Blocked, not an empty result |
| 200 with a valid, empty list | No error. An empty Run is a success. |
| Something only this source does | Your own error |
The last two rows matter most. A challenge page served with HTTP 200 must never look like "no items", and a legitimately empty answer must never look like a failure.
Failures you did not expect
A thrown exception or a bug is a defect. It is not in the error union and is not something a caller can react to. Defects end the Run as failed.
Put the context of any failure on the trace, not in a side channel. Name steps with Effect.fn, add facts with Effect.annotateCurrentSpan, and put a link or a storage key on the span for anything large.
Errors on an instance
An instance stores a failed Run's error as a string in its result record. The string names the error's tag but not its fields. The full context is on the Run's trace.
Run the Scraper in process with Scraper.run to see the typed error with its reason and sample.
Source
- blocked.ts, rate-limited.ts, upstream-down.ts, quota-exhausted.ts, schema-drift.ts, invalid-input.ts
- client.test.ts: a Definition with its own error, decoded by the client