How to use the pg-promise.errors.QueryResultError function in pg-promise

To help you get started, we’ve selected a few pg-promise examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tomalrussell / colouring-london / app / src / api / services / passwordReset.ts View on Github external
// verify and deactivate the token in one operation
        const usedToken = await db.one(
            `UPDATE
                user_password_reset_tokens
            SET used = true
            WHERE
            token = $1::uuid
            AND NOT used
            AND now() at time zone 'utc' < expires_on
            RETURNING *`, [token]
            );
        console.log('verified');

        return usedToken.user_id;
    } catch (err) {
        if (err instanceof errors.QueryResultError)  {
            console.log(err.code);
            if (err.code === errors.queryResultErrorCode.noData) return undefined;
        }

        throw err;
    }
}
github tomalrussell / colouring-london / app / src / api / services / user.ts View on Github external
FROM users
            WHERE
            username = $1
            `, [
                username,
                password
            ]
        );

        if (user && user.auth_ok) {
            return { user_id: user.user_id }
        } else {
            return { error: 'Username or password not recognised' }
        }
    } catch(err) {
        if (err instanceof errors.QueryResultError) {
            console.error(`Authentication failed for user ${username}`);
            return { error: 'Username or password not recognised' };
        }
        console.error('Error:', err);
        return { error: 'Database error' }; 
    }
}