Skip to content

Commit

Permalink
Fix two issues around not checking for bad server responses
Browse files Browse the repository at this point in the history
Firstly, correctly set last sync status when told not to sync by server

Secondly, check status code before proceeding
  • Loading branch information
calpaterson committed Dec 18, 2020
1 parent fd01368 commit ebecd2a
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/extension/src/quarchive-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,38 @@ async function getHTTPConfig(): Promise<Array<string>> {
let gettingURL = await browser.storage.sync.get("APIURL");
const returnValue = [gettingURL.APIURL, gettingUsername.username, gettingKey.APIKey];
if (returnValue.includes(undefined)) {
throw new NoConfigurationError();
throw new NoConfigurationError("no or missing configuration");
} else {
return returnValue;
}
}

class NoConfigurationError extends Error {
constructor(message?: string) {
constructor(message: string) {
super(message);
Object.setPrototypeOf(this, new.target.prototype);
this.name = NoConfigurationError.name
}
}

class BadServerResponseError extends Error {
response: Response
constructor(message: string, response: Response) {
super(message);
Object.setPrototypeOf(this, new.target.prototype);
this.name = BadServerResponseError.name
this.response = response
}
}

function throwForStatus(response: Response): void {
if(!response.ok){
console.warn("got %d from server", response.status);
throw new BadServerResponseError(`bad response: (${response.status})`, response);
}
}


async function setLastFullSyncResult(result: SyncResult): Promise<void> {
const storable = {
"status": result.status,
Expand Down Expand Up @@ -309,6 +327,7 @@ async function callSyncAPI(bookmark: Bookmark): Promise<Array<Bookmark>> {
},
body: sync_body,
});
throwForStatus(response);

const jsonlines = await response.text()
let returnValue = [];
Expand Down Expand Up @@ -351,6 +370,7 @@ async function callFullSyncAPI(bookmarks: Array<Bookmark>): Promise<Array<Bookma
},
body: body.join("\n"),
});
throwForStatus(response);
const jsonlines = await response.text()
console.timeEnd(timerString);
let returnValue = [];
Expand Down Expand Up @@ -384,8 +404,9 @@ async function shouldSync(): Promise<boolean> {
"Quarchive-ClientID": clientID,
}
});
throwForStatus(response);
const json = await response.json()
return json.should_sync;
return json.should_sync === true;
}

export async function fullSync(force: boolean = false): Promise<SyncResult> {
Expand All @@ -404,13 +425,12 @@ export async function fullSync(force: boolean = false): Promise<SyncResult> {
// status back
if (!force && !await shouldSync()) {
console.log("no need to sync yet")
status = oldStatus;
setLastFullSyncResult(oldStatus);
} else {
if (force) {
console.warn("forcing sync");
}


// Then retrieve the server's point of view
const bookmarksFromServer = await callFullSyncAPI(await allBookmarksFromLocalDb());

Expand Down

0 comments on commit ebecd2a

Please sign in to comment.