Request a demo

BigQuery · Utility function

BigQuery ERROR

ERROR(message) stops the query with your own error message. Use it inside IF or CASE to fail a query when a data check doesn't pass, such as an empty partition or a duplicate key, so scheduled queries and pipelines stop instead of writing bad data.

GoogleSQL for BigQuery · Checked against the official reference in November 2026

ERROR raises a runtime error whose message is the string you pass, and the job fails. Its return type adapts to the context, so it fits in any branch of IF or CASE next to values of any type: IF(amount >= 0, amount, ERROR('negative amount')). The message must not be NULL: ERROR(NULL) fails with Input to ERROR function must not be NULL, which hides your message when you build it with CONCAT from a NULL column.

This turns any SELECT into a data test. Scheduled queries, Dataform or dbt runs and Airflow tasks see the failed job as a failure, so a check for an empty partition, duplicate keys or an unmapped category stops the pipeline and fires its alerting. On on-demand pricing a failed query isn't charged; see BigQuery pricing.

BigQuery gives ERROR no special evaluation order. In WHERE x > 0 AND ERROR('...') the ERROR may run before the comparison and fail on rows you meant to skip; put it inside IF or CASE, which only use the chosen branch. In scripts, the ASSERT statement and RAISE USING MESSAGE fail a script the same way, and BEGIN ... EXCEPTION WHEN ERROR THEN ... END catches errors.

Syntax

ERROR(error_message)

Arguments and return value

ArgumentDescription
error_messageA STRING with the message to show. Must not be NULL.

Returns: Never returns a value: the query fails. The return type is inferred from the context, so ERROR fits in any branch of IF or CASE.

Examples

Fail on an unmapped category

The query fails with the message Unmapped payment method: crypto. Without the ERROR branch, crypto payments would silently get a NULL group.

WITH payments AS (
  SELECT 'p1' AS payment_id, 'card' AS method UNION ALL
  SELECT 'p2', 'paypal' UNION ALL
  SELECT 'p3', 'crypto'
)
SELECT
  payment_id,
  CASE method
    WHEN 'card' THEN 'cards'
    WHEN 'paypal' THEN 'wallets'
    ELSE ERROR(CONCAT('Unmapped payment method: ', method))
  END AS method_group
FROM payments;

A check that passes returns a value

No amount is negative, so IF returns the row count and the query succeeds.

WITH orders AS (
  SELECT 'o1' AS order_id, 120 AS amount UNION ALL
  SELECT 'o2', 80
)
SELECT
  IF(COUNTIF(amount < 0) = 0, COUNT(*), ERROR('orders: negative amounts found')) AS checked_rows
FROM orders;
Result
checked_rows
2

Volume check on yesterday's partition

Run it as a scheduled query or a pipeline step after the load. If yesterday's partition is empty, the job fails with a message such as orders: no rows for 2026-11-22.

SELECT
  IF(
    COUNT(*) > 0,
    COUNT(*),
    ERROR(FORMAT('orders: no rows for %t', DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY)))
  ) AS loaded_rows
FROM `project.dataset.orders`
WHERE order_date = DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY);

Fail on duplicate keys

c2 appears twice, so the query fails with customers: 1 duplicate ids.

WITH customers AS (
  SELECT 'c1' AS customer_id UNION ALL
  SELECT 'c2' UNION ALL
  SELECT 'c2'
)
SELECT
  IF(
    COUNT(*) = COUNT(DISTINCT customer_id),
    'ok',
    ERROR(FORMAT('customers: %d duplicate ids', COUNT(*) - COUNT(DISTINCT customer_id)))
  ) AS uniqueness_check
FROM customers;

Common mistakes

  • Putting ERROR in a WHERE clause next to AND or OR. BigQuery doesn't guarantee the order, so ERROR can fire for rows another condition would exclude. Wrap it: WHERE IF(x > 0, TRUE, ERROR('x must be positive')).
  • Building the message from a NULL value. ERROR(CONCAT('bad id: ', id)) with a NULL id fails with Input to ERROR function must not be NULL instead of your message; FORMAT('bad id: %t', id) prints NULL instead.
  • Expecting partial results. The first row that reaches ERROR fails the whole job, nothing is returned or written, and later statements in a script don't run unless you catch the error with BEGIN ... EXCEPTION.
  • Trying SAFE.ERROR. It fails with SAFE with function error is not supported; to make a check optional, change the condition instead.

FAQ

How do I make a BigQuery query fail on purpose?

Call ERROR inside a conditional: SELECT IF(COUNT(*) > 0, 'ok', ERROR('table is empty')) FROM t. In a script you can also use ASSERT condition AS 'message' or RAISE USING MESSAGE = 'message'.

What is the difference between ERROR and ASSERT in BigQuery?

ERROR is a function, so it works inside any expression and query, including views and scheduled queries. ASSERT is a statement for scripts: it fails when its expression is FALSE or NULL and is billed like the equivalent SELECT.

Is a query that fails with ERROR billed?

On on-demand pricing, failed queries are not charged. With capacity (editions) pricing you pay for slots rather than per query, so the slot time the query used before failing is part of your usage.

How do I catch an error in a BigQuery script?

Wrap the statements in BEGIN ... EXCEPTION WHEN ERROR THEN ... END; and read @@error.message in the handler to log or re-raise the error.

More hash and utility functions

All BigQuery hash and utility functions · All functions · BigQuery error messages

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.