The THROW statement can be used to throw an error in a place where something unexpected is happening. Execution of the query will be aborted and the error will be returned to the client. While a string is most commonly seen after a THROW statement, any value at all can be used as error output.
Statement syntax
SurrealQL Syntax
THROW@error
Example usage
The following query shows example usage of this statement.
-- Throw an error THROW"some error message";
The following query shows the THROW statement being used to send back a custom error to the client.
-- In this example, we throw a custom error when a user provides invalid signin details DEFINEACCESSuserONDATABASETYPERECORD SIGNIN{ LET$user = (SELECT * FROMuserWHEREusername=$usernameANDcrypto::argon2::compare(password, $password)); IF !$user{ THROW"You either provided invalid credentials, or a user with the username "+<string>$username+" might not exist."; };
RETURN$user; } DURATIONFORSESSION1w ;
THROW can contain any value: arrays, objects, and so on. It can even take the value of a separate SELECT statement: