Skip to content

Commit b9d504e

Browse files
committed
Updated OAuth2 error formatting
1 parent 5f799da commit b9d504e

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

lib/oauth/gmail.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,20 @@ const checkForFlags = (err, isPrincipal) => {
1919
return false;
2020
}
2121

22-
let { error, error_description: description } = err;
22+
const { error, error_description: description } = err;
2323

2424
if (error && typeof error === 'object') {
2525
let activationLink;
2626
if (/API has not been used/.test(error.message) && Array.isArray(error.details)) {
27-
for (let detail of error.details) {
27+
for (const detail of error.details) {
2828
if (!detail || !Array.isArray(detail.links)) {
2929
continue;
3030
}
3131
activationLink = (detail.links.find(link => /Google developers console API activation/.test(link.description) && link.url) || {}).url;
3232
if (activationLink) {
3333
return {
34-
message: 'Gmail API needs to be enabled before it can be used',
34+
message: 'Please enable the Gmail API in your Google Developer Console to use this feature.',
35+
code: 'GMAIL_API_NOT_ENABLED',
3536
url: activationLink
3637
};
3738
}
@@ -40,44 +41,54 @@ const checkForFlags = (err, isPrincipal) => {
4041

4142
if (/insufficient authentication scopes/i.test(error.message)) {
4243
return {
43-
message: 'EmailEngine can’t process Gmail API requests because the configured OAuth2 permission scopes are insufficient'
44+
message:
45+
'The current OAuth2 permission scopes are not sufficient to process Gmail API requests. Please update the scopes in your configuration.',
46+
code: 'INSUFFICIENT_AUTH_SCOPES'
4447
};
4548
}
4649

4750
if (/Invalid topicName does not match/.test(error.message)) {
4851
return {
49-
message: 'Cloud Pub/Sub and Gmail API OAuth2 applications must be for the same project'
52+
message:
53+
'There is a mismatch between your Cloud Pub/Sub configuration and the Gmail API OAuth2 application project. Please ensure both are associated with the same project.',
54+
code: 'PROJECT_MISMATCH'
5055
};
5156
}
5257
}
5358

5459
if (error === 'invalid_client' && /The OAuth client was not found/i.test(description)) {
5560
return {
56-
message: 'OAuth Client ID for Google is invalid'
61+
message: 'The OAuth Client ID is invalid. Please verify that your Google integration is configured with the correct Client ID.',
62+
code: 'INVALID_CLIENT_ID'
5763
};
5864
}
5965

6066
if (error === 'invalid_client' && /Unauthorized/i.test(description)) {
6167
return {
62-
message: 'OAuth Client Secret for Google is invalid'
68+
message: 'The OAuth Client Secret is incorrect. Please verify that you have provided the correct Client Secret for your Google integration.',
69+
code: 'INVALID_CLIENT_SECRET'
6370
};
6471
}
6572

6673
if (error === 'unauthorized_client' && /Client is unauthorized to retrieve access tokens/i.test(description)) {
6774
return {
68-
message: 'Verify OAuth2 scopes and domain-wide delegation setup for you project'
75+
message:
76+
'The client is not authorized to retrieve access tokens. Check your OAuth2 scopes and ensure domain-wide delegation is set up correctly for your project.',
77+
code: 'UNAUTHORIZED_CLIENT'
6978
};
7079
}
7180

7281
if (error === 'invalid_request' && /Invalid principal/i.test(description)) {
7382
return {
74-
message: 'OAuth Client Email for Google is invalid'
83+
message: 'The OAuth Client Email provided is invalid. Please review your Google integration settings and correct the email if needed.',
84+
code: 'INVALID_CLIENT_EMAIL'
7585
};
7686
}
7787

7888
if (isPrincipal && error === 'invalid_grant' && /account not found/i.test(description)) {
7989
return {
80-
message: 'Invalid Service Client Email'
90+
message: 'The Service Client Email is invalid or unrecognized. Please verify that your service account credentials are correct.',
91+
code: 'INVALID_SERVICE_CLIENT_EMAIL'
8192
};
8293
}
8394

@@ -495,8 +506,11 @@ class GmailOauth {
495506
if (flag) {
496507
await this.setFlag(flag);
497508
err.tokenRequest.flag = flag;
509+
if (flag.code && !err.code) {
510+
err.code = flag.code;
511+
}
498512
}
499-
} catch (err) {
513+
} catch (E) {
500514
// ignore
501515
} finally {
502516
if (this.logger) {

lib/oauth/outlook.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,27 @@ const checkForFlags = err => {
5959
return false;
6060
}
6161

62-
let { error, error_description: description } = err;
62+
const { error, error_description: description } = err;
6363

6464
if (error === 'invalid_client' && /AADSTS7000222/i.test(description)) {
6565
return {
66-
message: 'OAuth Client Secret for Outlook is either expired or not yet valid',
66+
message: 'The Outlook OAuth Client Secret is either expired or not yet valid. Please update your secret accordingly.',
67+
code: 'OUTLOOK_CLIENT_SECRET_EXPIRED_OR_NOT_VALID',
6768
description
6869
};
6970
}
7071

7172
if (error === 'invalid_client' && /AADSTS700016/i.test(description)) {
7273
return {
73-
message: 'OAuth Application ID for Outlook is invalid'
74+
message: 'The Outlook OAuth Application ID provided is invalid. Please verify your Application ID configuration.',
75+
code: 'OUTLOOK_APP_ID_INVALID'
7476
};
7577
}
7678

7779
if (error === 'invalid_client' && /AADSTS7000215/i.test(description)) {
7880
return {
79-
message: 'OAuth Client Secret for Outlook is invalid'
81+
message: 'The Outlook OAuth Client Secret is invalid. Please verify the secret and try again.',
82+
code: 'OUTLOOK_CLIENT_SECRET_INVALID'
8083
};
8184
}
8285

@@ -88,18 +91,21 @@ const checkForUserFlags = err => {
8891
return false;
8992
}
9093

91-
let { error, error_description: description } = err;
94+
const { error, error_description: description } = err;
9295

9396
if (error === 'invalid_grant' && /user might have changed or reset their password/i.test(description)) {
9497
return {
95-
message: 'The user changed their email account password and OAuth2 grant was revoked. Ask the user to re-authenticate.',
98+
message:
99+
"The user's password may have been changed or reset, causing the OAuth2 grant to be revoked. Please ask the user to re-authenticate their account.",
100+
code: 'OUTLOOK_USER_PASSWORD_CHANGED',
96101
description
97102
};
98103
}
99104

100105
if (error === 'invalid_grant') {
101106
return {
102-
message: 'Failed to renew the access token for the user',
107+
message: 'Failed to renew the Outlook access token for the user. Please have the user sign in again to reauthorize access.',
108+
code: 'OUTLOOK_TOKEN_RENEWAL_FAILED',
103109
description
104110
};
105111
}
@@ -469,8 +475,11 @@ class OutlookOauth {
469475
let flag = checkForFlags(err.oauthRequest.response);
470476
if (flag) {
471477
await this.setFlag(flag);
478+
if (flag.code && !err.code) {
479+
err.code = flag.code;
480+
}
472481
}
473-
} catch (err) {
482+
} catch (E) {
474483
// ignore
475484
} finally {
476485
if (this.logger) {

0 commit comments

Comments
 (0)