إنتقل إلى المحتوى الرئيسي

Error handling

Terra operations fail for a small number of well-defined reasons. The core library reports them as numeric codes; each binding surfaces them in whatever way is idiomatic for its language.

Error codes​

CodeNameMeaning
0E_SUCCESSOperation succeeded
1E_FAILEDOperation failed for a reason with no more specific code
2E_DOMAINAn argument was outside its valid domain
3E_LATLNG_DOMAINLatitude or longitude outside the valid range
4E_RES_DOMAINResolution outside 0–14
5E_CELL_INVALIDThe value is not a well-formed Terra cell
6E_DIR_EDGE_INVALIDThe value is not a well-formed directed edge
7E_RES_MISMATCHCells at different resolutions where equal resolution is required
8E_MEMORY_ALLOCMemory allocation failed
9E_MEMORY_BOUNDSOutput array too small for the result
10E_OPTION_INVALIDAn option or mode argument was not recognised
11E_EXTENTThe position falls outside the Terra coverage extent
12E_CHECK_FAILEDA Terra Grid Code failed check-character validation

Codes 11 and 12 have no equivalent in general-purpose grid libraries and deserve attention.

E_EXTENT — outside coverage​

Terra is a regional system. It covers Saudi Arabia and a GCC buffer, not the world. A coordinate in Cairo or Karachi is a valid coordinate and has no Terra cell.

This is the error most likely to surprise a new integration, because it is usually a symptom rather than a cause. The common triggers:

  • Latitude and longitude transposed. (46.72, 24.68) instead of (24.68, 46.72) lands in the Indian Ocean. This is by far the most frequent cause.
  • A datum or transformation error large enough to move the point out of the region.
  • A sentinel value such as (0, 0) reaching the indexer from an incomplete record.
  • Genuinely out-of-region data, which is a data scoping question rather than a defect.

Treat a sudden rise in E_EXTENT during a bulk load as an ingest problem, not as legitimately foreign data, until proven otherwise.

E_CELL_INVALID — malformed cell​

A 64-bit value is not a Terra cell merely by being 64 bits. Validation checks that the mode is correct, reserved fields are zero, the resolution is 0–14, the base cell exists, digits at or below the resolution are 0–6, and digits beyond it are the unused marker 7.

The usual causes:

  • Hand-constructed indices where the resolution field was changed without updating the digits, or a digit was written without clearing the field first. Unused digits are 7, all bits set, so a bitwise OR cannot change them — the field must be cleared, then written.
  • Truncation through a type that cannot hold 64 bits. In JavaScript every Terra index exceeds Number.MAX_SAFE_INTEGER; storing one in a Number corrupts it silently. Use BigInt or the string form.
  • String round-trips that dropped the leading t or lost a character.

E_CHECK_FAILED — bad grid code​

A Terra Grid Code failed its check character. This means a transcription error, not an unknown location.

Do not attempt correction. The check character detects errors; it does not identify which character is wrong, and a plausible-looking correction may be a different real location. Reject the code and ask for it again, indicating which part failed so the person knows what to re-read.

How each binding reports errors​

C — Functions return a TerraError; results are written through an out-parameter. Always check the return value before reading the result.

TerraIndex cell;
TerraError err = latLngToCell(&coord, 8, &cell);
if (err != E_SUCCESS) { /* handle */ }

Python — Raises TerraError, or a subclass such as TerraDomainError or TerraExtentError. The numeric code is available on the exception.

try:
cell = terra.latlon_to_cell(lat, lon, 8)
except terra.TerraExtentError:
...

JavaScript — Throws an Error carrying a terraCode property.

Go — Returns (result, error) in the conventional style. Errors are comparable against the package's sentinel values.

PostgreSQL — Raises a SQL exception. Functions with an _or_null suffix return NULL instead, which is usually what you want when indexing a column that may contain bad rows.

SELECT terra_latlon_to_cell_or_null(lat, lon, 8) FROM points;

R — Returns NA for individual failures within a vectorised call, and signals a condition for whole-call failures.

DuckDB — Returns NULL for row-level failures, consistent with its handling of other scalar functions.

Handling errors in bulk work​

When indexing a large table, a single bad row should not abort the load. Prefer the null-returning variants, then count and inspect the nulls:

SELECT count(*) FILTER (WHERE cell IS NULL) AS failed,
count(*) AS total
FROM indexed_points;

A small null count is usually bad source records. A large one is usually a systematic problem — transposed coordinates, wrong datum, or wrong column — and re-running the load without diagnosing it will produce a confidently wrong dataset.

Next​


The Terra System is designed and developed by Tec Solution KSA.