Skip to content

Commit d85be20

Browse files
authored
Merge pull request #289 from OneNoteDev/bug/fetch-new-user-token-on-expiry-mid-patch
If the user token expires mid-patch clip, refresh it
2 parents 22777c7 + 8ad3fe3 commit d85be20

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

src/scripts/saveToOneNote/saveToOneNote.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {Constants} from "../constants";
22
import {PromiseUtils} from "../promiseUtils";
33
import {Settings} from "../settings";
44
import {StringUtils} from "../stringUtils";
5+
import {UserInfo} from "../userInfo";
56

67
import {Clipper} from "../clipperUI/frontEndGlobals";
78

@@ -179,19 +180,45 @@ export class SaveToOneNote {
179180

180181
Promise.all([getRevisionsPromise, timeoutPromise]).then((values) => {
181182
let revisions = values[0] as OneNoteApi.Revision[];
182-
this.getApi().updatePage(pageId, revisions).then(() => {
183+
184+
let thenCb = () => {
183185
timeBetweenPatchRequests = SaveToOneNote.timeBetweenPatchRequests;
184186
resolve();
185-
}).catch((error) => {
186-
reject(error);
187-
});
187+
};
188+
189+
let catchCb = (error: OneNoteApi.RequestError) => {
190+
if (error.statusCode === 401) {
191+
// The clip has taken a really long time and the user token has expired
192+
Clipper.getExtensionCommunicator().callRemoteFunction(Constants.FunctionKeys.ensureFreshUserBeforeClip, {
193+
callback: (updatedUser: UserInfo) => {
194+
if (updatedUser.user.accessToken) {
195+
// Try again with the new token
196+
this.accessToken = updatedUser.user.accessToken;
197+
this.updatePage(pageId, revisions, thenCb, catchCb);
198+
} else {
199+
reject(error);
200+
}
201+
}
202+
});
203+
} else {
204+
reject(error);
205+
}
206+
};
207+
208+
this.updatePage(pageId, revisions, thenCb, catchCb);
188209
});
189210
});
190211
});
191212
}, this.getApi().getPageContent(pageId)) // Check if page exists with retries
192213
]);
193214
}
194215

216+
private updatePage(pageId: string, revisions: OneNoteApi.Revision[], thenCb: () => void, catchCb: (error: OneNoteApi.RequestError) => void) {
217+
this.getApi().updatePage(pageId, revisions)
218+
.then(thenCb)
219+
.catch(catchCb);
220+
}
221+
195222
// We try not and put logging logic in this class, but since we lazy-load images, this has to be an exception
196223
private getPatchWithLogging(saveable: OneNoteSaveable, index: number): Promise<OneNoteApi.Revision[]> {
197224
let event = new Log.Event.PromiseEvent(Log.Event.Label.ProcessPdfIntoDataUrls);

0 commit comments

Comments
 (0)