hediet / ts-typed-sql

A fully typed sql builder. Not maintained anymore, use it for your inspiration.
https://hediet.github.io/ts-typed-sql/
54 stars 4 forks source link

Typed Errors #27

Open phiresky opened 7 years ago

phiresky commented 7 years ago

Currently errors are thrown as normal errors: throw new Error("Expected at least one row.");

It would be good if they had some typed metadata that could be used as follows:

import {from, isTypedError} from "@hediet/typed-sql";

try {
    await db.exec(from(....));
} catch(e) {
    if (!isTypedError(e)) throw e;
    // e is fully typed here, and has meta data such as the sql error code / constraint name or the cause why typed-sql threw it.
}

implementation suggestion

typedErrorSymbol = Symbol("typedError");
export class TypedError extends Error {
    [typedErrorSymbol]: true;
    constructor(...) {}
}

export function isTypedError(e: any): e is TypedError {
    return e && e[typedErrorSymbol];
}