Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing/more errors #82

Open
CanRau opened this issue Feb 9, 2023 · 3 comments
Open

Missing/more errors #82

CanRau opened this issue Feb 9, 2023 · 3 comments

Comments

@CanRau
Copy link

CanRau commented Feb 9, 2023

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.

@ImLunaHey
Copy link

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;
};

@iheanyi
Copy link
Member

iheanyi commented Jul 31, 2023

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
Copy link
Author

CanRau commented Aug 1, 2023

yea I see, nothing urgent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants