Use SpectralError and ConnectionError to surface structured, identifiable errors from custom connectors.
The Spectral SDK provides two error classes — SpectralError and ConnectionError — and corresponding type guards that let you differentiate Spectral-originated errors from generic JavaScript errors in error handlers.
import { SpectralError, ConnectionError, isSpectralError, isConnectionError } from "@prismatic-io/spectral";
A base error class that extends the built-in Error. All SpectralError instances carry an isSpectralError: true flag, which allows the isSpectralError type guard to identify them reliably — even when errors cross module boundaries or are serialized.
export class SpectralError extends Error { isSpectralError: boolean; // always true constructor(message: string) { super(message); this.isSpectralError = true; this.name = this.constructor.name; // "SpectralError" or subclass name Error.captureStackTrace(this, this.constructor); }}
Extends SpectralError and attaches the Connection object that caused the failure. This makes it possible for error handlers to inspect and log which connection was at fault without needing to carry that information in the message string.
Throw a ConnectionError inside an action’s perform function when a connection fails an authentication or reachability check:
import { action, ConnectionError } from "@prismatic-io/spectral";import { createClient, toAuthorizationHeaders } from "@prismatic-io/spectral/dist/clients/http";export const listUsers = action({ display: { label: "List users", description: "Return all users" }, inputs: { connection: { label: "Connection", type: "connection", required: true }, }, perform: async (context, { connection }) => { const client = createClient({ baseUrl: "https://api.acme.com/v2/", headers: toAuthorizationHeaders(connection), }); try { const { data } = await client.get("/users"); return { data }; } catch (error: unknown) { if ((error as any)?.response?.status === 401) { throw new ConnectionError( connection, "Authentication failed. Verify your API credentials are correct.", ); } throw error; } },});
ConnectionError stores the full Connection object on error.connection. Avoid logging the full object to prevent leaking credentials — log only error.connection.key or error.connection.label.
Use for general connector errors that are not tied to a specific connection — for example, a missing required input, a failed data transformation, or an unsupported operation.
ConnectionError
Use when a request fails because a connection is invalid, expired, or misconfigured. Attaching the connection lets downstream error handlers surface the exact credential that needs attention.
Always throw a ConnectionError (not a plain Error) for authentication failures. This allows Prismatic’s platform to surface actionable connection re-authorization prompts to end users.