Skip to content

Commit 7ffcaa0

Browse files
committed
Improvements to promise rejection and error handling
1 parent bdb0382 commit 7ffcaa0

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

src/index.ts

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,33 @@ export async function authorize(
5252
POPUP_STATE.open = true;
5353

5454
if (!configuration.domain) {
55-
await openDomainPopUp(configuration, popup).then((result: Token): Token | void => {
56-
POPUP_STATE.open = false;
57-
token = result;
58-
});
59-
} else {
60-
await authenticate(configuration as AuthConfiguration, popup).then((result: Token): Token | void => {
61-
POPUP_STATE.open = false;
62-
if (result) {
55+
await openDomainPopUp(configuration, popup)
56+
.then((result: Token): Token | void => {
57+
POPUP_STATE.open = false;
6358
token = result;
64-
}
65-
});
59+
})
60+
.catch((error) => {
61+
if (error === false) {
62+
throw new AuthenticatorError('ERR_DOMAIN_POPUP_CLOSED', 'Domain cancelled by client.');
63+
}
64+
65+
throw new AuthenticatorError('ERR_AUTH_FAILED', 'Auth failed.');
66+
});
67+
} else {
68+
await authenticate(configuration as AuthConfiguration, popup)
69+
.then((result: Token): Token | void => {
70+
POPUP_STATE.open = false;
71+
if (result) {
72+
token = result;
73+
}
74+
})
75+
.catch((error) => {
76+
if (error === false) {
77+
throw new AuthenticatorError('ERR_AUTH_POPUP_CLOSED', 'Auth aborted by client.');
78+
}
79+
80+
throw new AuthenticatorError('ERR_AUTH_FAILED', 'Auth failed.');
81+
});
6682
}
6783

6884
if (!token) {
@@ -151,10 +167,7 @@ function openDomainPopUp(configuration: AuthConfigurationInput, popUp: Popup): P
151167
POPUP_STATE.open = false;
152168
clearTimeout(domainPopUpTimeout);
153169
popUp.close();
154-
logMessage('warning', {
155-
code: 'WARN_DOMAIN_POPUP_CLOSED',
156-
message: 'Domain popup closed.',
157-
});
170+
reject(false);
158171
});
159172
});
160173
}
@@ -167,7 +180,7 @@ function openAuthPopUp(url: string, popUp: Popup): Promise<void> {
167180
message: 'Auth popup opened.',
168181
});
169182

170-
return new Promise((resolve) => {
183+
return new Promise((resolve, reject) => {
171184
authTimeout = setTimeout(() => {
172185
POPUP_STATE.open = false;
173186
popUp.close();
@@ -181,10 +194,7 @@ function openAuthPopUp(url: string, popUp: Popup): Promise<void> {
181194
POPUP_STATE.open = false;
182195
clearTimeout(authTimeout);
183196
popUp.close();
184-
logMessage('warning', {
185-
code: 'WARN_AUTH_POPUP_CLOSED',
186-
message: 'Auth popup closed.',
187-
});
197+
reject(false);
188198
});
189199

190200
popUp.onSuccess(() => {
@@ -202,10 +212,7 @@ function openAuthPopUp(url: string, popUp: Popup): Promise<void> {
202212
POPUP_STATE.open = false;
203213
clearTimeout(authTimeout);
204214
popUp.close();
205-
logMessage('warning', {
206-
code: 'WARN_AUTH_CANCELLED',
207-
message: 'Auth cancelled.',
208-
});
215+
reject(false);
209216
});
210217
});
211218
}

0 commit comments

Comments
 (0)