Connectum API Reference / @connectum/test-fixtures / index / assertConnectError
Function: assertConnectError() ​
assertConnectError(
error,expectedCode,messagePattern?):asserts error is ConnectError
Defined in: assertions.ts:50
Assert that a thrown value is a ConnectError with the expected gRPC status code and, optionally, a message matching a pattern.
This is a TypeScript assertion function: after a successful call the compiler narrows error to ConnectError.
Note on message format: ConnectError messages include a code prefix, e.g. [not_found] user not found. A supplied messagePattern is matched against the full message, but matching is refused when it exceeds 1000 characters. Use a RegExp for flexible matching within that limit.
Parameters ​
error ​
unknown
The value to check (typically from a catch block).
expectedCode ​
Code
Expected gRPC/Connect status code.
messagePattern? ​
string | RegExp
Optional substring or RegExp to match against error.message.
Returns ​
asserts error is ConnectError
Throws ​
When messagePattern is given and is neither a string nor a RegExp (a number or a plain object would otherwise skip the message check and pass).
Throws ​
When any of the checks fail. A value named ConnectError that is not an instance of this package's class (an error from another installed copy of @connectrpc/connect) fails with a message that says so.
Example ​
import { Code, ConnectError } from "@connectrpc/connect";
import { assertConnectError } from "@connectum/testing";
try {
await client.getUser({ id: "missing" });
} catch (err) {
assertConnectError(err, Code.NotFound, "user not found");
// err is now typed as ConnectError
console.log(err.code); // Code.NotFound
}