Skip to content

Commit 29da53f

Browse files
committed
fix: enrich OAuth token error messages for BullMQ job visibility
Compose descriptive err.message at error creation in OAuth provider classes so that BullMQ failedReason includes provider name, grant type, HTTP status, and provider error response instead of just "Token request failed".
1 parent 4e48007 commit 29da53f

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

lib/oauth/gmail.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const packageData = require('../../package.json');
4-
const { formatPartialSecretKey, structuredClone, fetchAgent, retryAgent } = require('../tools');
4+
const { formatPartialSecretKey, structuredClone, fetchAgent, retryAgent, formatTokenError } = require('../tools');
55
const crypto = require('crypto');
66

77
const { fetch: fetchCmd } = require('undici');
@@ -342,6 +342,7 @@ class GmailOauth {
342342
// ignore
343343
}
344344

345+
err.message = formatTokenError(this.provider, err.tokenRequest);
345346
throw err;
346347
}
347348

@@ -460,6 +461,7 @@ class GmailOauth {
460461
} catch (e) {
461462
// ignore
462463
}
464+
err.message = formatTokenError(this.provider, err.tokenRequest);
463465
throw err;
464466
}
465467

lib/oauth/mail-ru.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const packageData = require('../../package.json');
44
const { fetch: fetchCmd } = require('undici');
5-
const { formatPartialSecretKey, structuredClone, fetchAgent, retryAgent } = require('../tools');
5+
const { formatPartialSecretKey, structuredClone, fetchAgent, retryAgent, formatTokenError } = require('../tools');
66

77
const MAIL_RU_SCOPES = ['userinfo', 'mail.imap'];
88

@@ -169,6 +169,7 @@ class MailRuOauth {
169169
} catch (e) {
170170
// ignore
171171
}
172+
err.message = formatTokenError(this.provider, err.tokenRequest);
172173
throw err;
173174
}
174175

@@ -253,6 +254,7 @@ class MailRuOauth {
253254
} catch (e) {
254255
// ignore
255256
}
257+
err.message = formatTokenError(this.provider, err.tokenRequest);
256258
throw err;
257259
}
258260

lib/oauth/outlook.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const packageData = require('../../package.json');
4-
const { formatPartialSecretKey, structuredClone, fetchAgent, retryAgent } = require('../tools');
4+
const { formatPartialSecretKey, structuredClone, fetchAgent, retryAgent, formatTokenError } = require('../tools');
55

66
const { fetch: fetchCmd } = require('undici');
77

@@ -324,6 +324,7 @@ class OutlookOauth {
324324
} catch (e) {
325325
// ignore
326326
}
327+
err.message = formatTokenError(this.provider, err.tokenRequest);
327328
throw err;
328329
}
329330

@@ -424,6 +425,7 @@ class OutlookOauth {
424425
} catch (e) {
425426
// ignore
426427
}
428+
err.message = formatTokenError(this.provider, err.tokenRequest);
427429
throw err;
428430
}
429431

lib/schemas.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,9 @@ const licenseSchema = Joi.object({
11061106
}).label('LicenseInfo');
11071107

11081108
const lastErrorSchema = Joi.object({
1109-
response: Joi.string().example('Token request failed').description('Human-readable error message'),
1109+
response: Joi.string()
1110+
.example('Token request failed for gmail (refresh_token, HTTP 400): invalid_grant - Token has been expired or revoked.')
1111+
.description('Human-readable error message'),
11101112
serverResponseCode: Joi.string().example('OauthRenewError').description('Error code or classification'),
11111113
tokenRequest: Joi.object({
11121114
grant: Joi.string()

lib/tools.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@ class LRUCache extends Map {
7777

7878
const regexCache = new LRUCache(1000);
7979

80+
function formatTokenError(provider, tokenRequest) {
81+
let parts = [`Token request failed for ${provider}`];
82+
if (tokenRequest) {
83+
let detail = `${tokenRequest.grant || 'unknown'}, HTTP ${tokenRequest.status || '?'}`;
84+
parts[0] += ` (${detail})`;
85+
if (tokenRequest.response) {
86+
let resp = tokenRequest.response;
87+
let errorParts = [resp.error, resp.error_description].filter(Boolean);
88+
if (errorParts.length) {
89+
parts.push(errorParts.join(' - '));
90+
}
91+
}
92+
}
93+
return parts.join(': ');
94+
}
95+
8096
module.exports = {
8197
/**
8298
* Helper function to set specific bit in a buffer
@@ -1785,7 +1801,9 @@ MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEV3QUiYsp13nD9suD1/ZkEXnuMoSg
17851801

17861802
LRUCache,
17871803

1788-
normalizeHashKeys
1804+
normalizeHashKeys,
1805+
1806+
formatTokenError
17891807
};
17901808

17911809
function msgpackDecode(buf) {

0 commit comments

Comments
 (0)