# Postgres error P0001: raise_exception (a RAISE EXCEPTION in PL/pgSQL)

> Your own function or trigger raised an error on purpose with RAISE EXCEPTION.

Source: https://scribase.com/postgres-errors/p0001

SQLSTATE class: PL/pgSQL Error. Over PostgREST/supabase-js this maps to HTTP 400.

## Causes

- A validation inside a trigger or function failed, and the message is whatever that code wrote.

## Fixes

- Read the message and the function named in the CONTEXT line; fix the input or the rule.
- Raise with your own SQLSTATE and HINT so clients can tell errors apart: raise exception 'Out of stock' using errcode = 'P0001', hint = 'Reduce the quantity';

## Example

```sql
create function check_stock() returns trigger language plpgsql as $$
begin
  if new.qty > (select stock from products where id = new.product_id) then
    raise exception 'Not enough stock for product %', new.product_id
      using hint = 'Lower the quantity';
  end if;
  return new;
end $$;
```

PostgREST answers 400 and passes your message and hint to the client.