Better handle editUsername when Auth0 update fails
Hmm, right, okay, we *generally* should have all users imported to Auth0, but this can fail if the cron job is behind or Auth0 rejected the data (e.g. user data in a format it doesn't support). Previously, this would apply the name change in the database, but return Auth0's "The user does not exist." error to the GraphQL client, making it look like the update fully failed. In this change, we handle that case differently: when the Auth0 update fails with a 404, we proceed but log a warning; and when Auth0 fails with an unexpected error, we roll back the database change in addition to raising the error to the client, to keep the behavior obvious and consistent.
This commit is contained in:
parent
b5865bf699
commit
2fb84b0b0f
1 changed files with 63 additions and 26 deletions
|
@ -872,6 +872,10 @@ const resolvers = {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await db.beginTransaction();
|
||||||
|
|
||||||
|
let auth0Warning = null;
|
||||||
|
try {
|
||||||
const [[result1, result2]] = await db.query(
|
const [[result1, result2]] = await db.query(
|
||||||
`
|
`
|
||||||
UPDATE users SET name = ? WHERE id = ? LIMIT 1;
|
UPDATE users SET name = ? WHERE id = ? LIMIT 1;
|
||||||
|
@ -900,13 +904,45 @@ const resolvers = {
|
||||||
// I'm not going to bother to write recovery code; in that case, the
|
// I'm not going to bother to write recovery code; in that case, the
|
||||||
// error will reach the support user console, and we can work to manually
|
// error will reach the support user console, and we can work to manually
|
||||||
// fix it.
|
// fix it.
|
||||||
|
try {
|
||||||
await getAuth0().users.update(
|
await getAuth0().users.update(
|
||||||
{ id: `auth0|impress-${userId}` },
|
{ id: `auth0|impress-${userId}` },
|
||||||
{ username: newUsername }
|
{ username: newUsername }
|
||||||
);
|
);
|
||||||
|
} catch (error) {
|
||||||
|
if (error.statusCode === 404) {
|
||||||
|
// If the user isn't in Auth0, that could be expected, if importing
|
||||||
|
// their record failed for some reason (unusually unsupported data
|
||||||
|
// values). Don't crash the endpoint, but do log a warning, in the
|
||||||
|
// server console and in Discord.
|
||||||
|
console.warn(`Auth0 warning: ${error.message}. Skipping.`);
|
||||||
|
auth0Warning = error;
|
||||||
|
} else {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// If any part of this fails (including the commit to Auth0), roll back
|
||||||
|
// the database change. This doesn't *super* matter exactly (it's
|
||||||
|
// probably not *bad* to change the username in the database), but it
|
||||||
|
// does make the results more obvious and consistent for Support staff.
|
||||||
|
await db.rollback();
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
await db.commit();
|
||||||
|
|
||||||
if (process.env["SUPPORT_TOOLS_DISCORD_WEBHOOK_URL"]) {
|
if (process.env["SUPPORT_TOOLS_DISCORD_WEBHOOK_URL"]) {
|
||||||
try {
|
try {
|
||||||
|
const auth0WarningFields = auth0Warning
|
||||||
|
? [
|
||||||
|
{
|
||||||
|
name:
|
||||||
|
"⚠ Auth0 warning, update skipped (maybe they weren't imported?)",
|
||||||
|
value: auth0Warning.message,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
: [];
|
||||||
await logToDiscord({
|
await logToDiscord({
|
||||||
embeds: [
|
embeds: [
|
||||||
{
|
{
|
||||||
|
@ -916,6 +952,7 @@ const resolvers = {
|
||||||
name: `Username`,
|
name: `Username`,
|
||||||
value: `${oldUser.name} → **${newUsername}**`,
|
value: `${oldUser.name} → **${newUsername}**`,
|
||||||
},
|
},
|
||||||
|
...auth0WarningFields,
|
||||||
],
|
],
|
||||||
timestamp: new Date().toISOString(),
|
timestamp: new Date().toISOString(),
|
||||||
url: `https://impress-2020.openneo.net/user/${oldUser.id}/items`,
|
url: `https://impress-2020.openneo.net/user/${oldUser.id}/items`,
|
||||||
|
|
Loading…
Reference in a new issue