Skip to content

Commit

Permalink
WK Bug fixes and better error logging (#374)
Browse files Browse the repository at this point in the history
* Clean up

* Added debugging logic

* Removed unused promise cache

* Added breadcrumbs
  • Loading branch information
ranger-ross authored Nov 25, 2022
1 parent 77c0dbd commit d38032e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 67 deletions.
22 changes: 22 additions & 0 deletions src/header/components/ClearCacheDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {useBunProPreloadStatus} from "../../hooks/useBunProPreloadStatus";
import {invalidWanikaniQueries} from "../../wanikani/service/WanikaniQueries";
import {invalidBunProQueries} from "../../bunpro/service/BunProQueries";
import {invalidAnkiQueries} from "../../anki/service/AnkiQueries";
import * as Sentry from "@sentry/react";

type DialogOptions = {
anki: boolean,
Expand All @@ -34,15 +35,36 @@ function usePurgeLocalData() {
const jobs = [];

if (options.anki) {
Sentry.addBreadcrumb({
level: 'info',
type: 'clear-cache',
data: {
app: 'anki'
}
});
jobs.push(invalidAnkiQueries());
}

if (options.bunPro) {
Sentry.addBreadcrumb({
level: 'info',
type: 'clear-cache',
data: {
app: 'bunpro'
}
});
jobs.push(invalidBunProQueries());
setBunProPreloadStatus(false);
}

if (options.wanikani) {
Sentry.addBreadcrumb({
level: 'info',
type: 'clear-cache',
data: {
app: 'wanikani'
}
});
jobs.push(invalidWanikaniQueries());
setWanikaniPreloadStatus(false);
}
Expand Down
27 changes: 0 additions & 27 deletions src/util/InMemoryCache.ts

This file was deleted.

24 changes: 0 additions & 24 deletions src/util/PromiseCache.ts

This file was deleted.

39 changes: 23 additions & 16 deletions src/wanikani/service/WanikaniApiServiceRxJs.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import * as localForage from "localforage";
import InMemoryCache from "../../util/InMemoryCache";
import {APP_URLS} from "../../Constants";
import {Observable, Subject} from "rxjs";
import {RawWanikaniReview} from "../models/raw/RawWanikaniReview";
import {sleep} from "../../util/ReactQueryUtils";

// @ts-ignore
const memoryCache = new InMemoryCache<any>();
import * as Sentry from "@sentry/react";

const wanikaniApiUrl = APP_URLS.wanikaniApi;
const cacheKeys = {
Expand Down Expand Up @@ -35,7 +32,7 @@ function fetchWithAutoRetry(input: string, init: RequestInit) {
async function tryRequest(attempts: number) {
const response = await fetch(input, init);

if (response.status == 429 && attempts < 10) {
if ([429, 401].includes(response.status) && attempts < 10) {
subject.next({
status: EVENT_STATUS.RATE_LIMITED,
});
Expand Down Expand Up @@ -106,10 +103,17 @@ function fetchMultiPageRequestObservable(path: string, startingId?: number) {

const firstPageResponse = event.response as Response;

const firstPage = await firstPageResponse.json();

let data = firstPage.data;
let nextPage = firstPage.pages['next_url']
let firstPage: any;
let nextPage: string;
let data: any;
try {
firstPage = await firstPageResponse.json();
data = firstPage.data;
nextPage = firstPage.pages['next_url']
} catch (err) {
Sentry.captureMessage(`Reviews page (first) exception, status ${firstPageResponse.status}`)
throw err;
}

function tryNextPage() {
fetchWithAutoRetry(nextPage, options)
Expand All @@ -123,9 +127,15 @@ function fetchMultiPageRequestObservable(path: string, startingId?: number) {

const pageResponse = event.response as Response;

const page = await pageResponse.json();
data = data.concat(page.data);
nextPage = page.pages['next_url'];
try {
const page = await pageResponse.json();
data = data.concat(page.data);
nextPage = page.pages['next_url'];
} catch (err) {
Sentry.captureMessage(`Reviews page exception, status ${pageResponse.status}`)
throw err;
}

if (!!nextPage) {
subject.next({
status: EVENT_STATUS.IN_PROGRESS,
Expand Down Expand Up @@ -180,17 +190,14 @@ function getReviews(): Observable<MultiPageObservableEvent<RawWanikaniReview>> {
const rateLimited = () => subject.next({status: EVENT_STATUS.RATE_LIMITED});

function handleEvent(event: MultiPageObservableEvent<RawWanikaniReview>, reviews: RawWanikaniReview[] = []) {
function save(partialData: RawWanikaniReview[], saveToMemCache = false) {
function save(partialData: RawWanikaniReview[]) {
const reviewsToSave = sortAndDeduplicateReviews([...reviews, ...partialData]);

const cacheObject = {
data: reviewsToSave,
lastUpdated: new Date().getTime(),
};
localForage.setItem(cacheKeys.reviews, cacheObject);
if (saveToMemCache) {
memoryCache.put(cacheKeys.reviews, cacheObject);
}
return cacheObject.data;
}

Expand Down

0 comments on commit d38032e

Please sign in to comment.