planetscale/database-js

Missing/more errors

CanRau opened this issue · 3 comments

CanRau commented

Thanks for this great library, it's fantastic to be able to query directly from Cloudflare Pages 🥳

Based on my experiments so far I feel like the library is pretty silent regarding errors, instead of returning or throwing anything it just does nothing, for example inserting a record with an existing id twice, only the first time shows the result of the query, second time nothing happens.

Personally I definitely prefer that it doesn't throw but it'd be really helpful if it could return errors, so I could react accordingly.

const result = await conn.execute(`INSERT INTO user (id, username) VALUES (?, ?)`, [id, username]);
console.log(JSON.stringify({ result }, null, 4));

Edit: just stumbled upon #62 😅 though as stated I'd prefer the former returning errors instead of throwing, or the option to configure this, in my case it does nothing where I expect some form of information.

Hopefully this helps. It should return a more detailed error.

const parseDatabaseError = (error: DatabaseError) => {
    try {
        const targetArray = error.body.message.split(': ');

        return {
            type: targetArray[2].split(': ')[0],
            code: targetArray[2].match(/code = (\w+)/)?.[1],
            description: targetArray[2].match(/desc = (.+?) \(errno/)?.[1],
            errno: targetArray[2].match(/\(errno (\d+)\)/)?.[1],
            sqlstate: targetArray[2].match(/\(sqlstate (\w+)\)/)?.[1],
            callerID: targetArray[2].match(/\(CallerID: (.+)\):/)?.[1],
            sql: targetArray[2].match(/Sql: "(.+?)"/)?.[1].replace(/\`/g, "'"),
            bindVars: targetArray[2].match(/BindVars: {(.+?)}/)?.[1],
        };
    } catch { }

    return null;
};

Hey, thanks for opening this issue. For now, I think what @ImLunaHey suggests is probably the best option. I briefly had downtime to sketch out making error handling configurable looks like, but ran into the problem of handling returned errors within transactions. If it was configurable, the errors could only be returned from direct execute calls, not necessarily within transactions., For returning errors, we'd have two different error-handling scenarios, one for returning them from calling execute outside of a transaction, and then throwing from inside of a transaction in order to always rollback. Even updating and changing the return type of transaction(fn) will also be a breaking change, so kind of requires more thought before we actually can implement this. 😅

CanRau commented

yea I see, nothing urgent