Skip to content

Commit

Permalink
Catch 401/403 responses in more robust way
Browse files Browse the repository at this point in the history
  • Loading branch information
jjnesbitt committed Nov 11, 2024
1 parent 0d4f603 commit a317f91
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
33 changes: 24 additions & 9 deletions web/src/stores/dandiset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ import { dandiRest, user } from '@/rest';
import type { User, Version } from '@/types';
import { draftVersion } from '@/utils/constants';


function isUnauthenticatedOrForbidden(err: unknown) {
return axios.isAxiosError(err) && err.response && [401, 403].includes(err.response.status);
}

interface State {
meta: {
dandisetExistsAndEmbargoed: boolean,
};
dandiset: Version | null;
versions: Version[] | null,
owners: User[] | null,
Expand All @@ -17,6 +25,7 @@ interface State {

export const useDandisetStore = defineStore('dandiset', {
state: (): State => ({
meta: { dandisetExistsAndEmbargoed: false },
dandiset: null,
versions: null,
owners: null,
Expand All @@ -40,6 +49,7 @@ export const useDandisetStore = defineStore('dandiset', {
},
actions: {
async uninitializeDandisets() {
this.meta.dandisetExistsAndEmbargoed = false;
this.dandiset = null;
this.versions = null;
this.owners = null;
Expand All @@ -60,10 +70,11 @@ export const useDandisetStore = defineStore('dandiset', {
try {
res = await dandiRest.versions(identifier);
} catch (err) {
// 401 errors are normal, and indicate that this dandiset is embargoed
// 401/403 errors are normal, and indicate that this dandiset is embargoed
// and the user doesn't have permission to view it.
if (axios.isAxiosError(err) && err.response?.status === 401) {
if (isUnauthenticatedOrForbidden(err)) {
res = null;
this.meta.dandisetExistsAndEmbargoed = true;
} else {
throw err;
}
Expand All @@ -74,19 +85,23 @@ export const useDandisetStore = defineStore('dandiset', {
}
},
async fetchDandiset({ identifier, version }: Record<string, string>) {
const sanitizedVersion = version || (await dandiRest.mostRecentVersion(identifier))?.version;

if (!sanitizedVersion) {
this.dandiset = null;
return;
}

try {
const sanitizedVersion = version || (await dandiRest.mostRecentVersion(identifier))?.version;
if (!sanitizedVersion) {
this.dandiset = null;
return;
}

const data = await dandiRest.specificVersion(identifier, sanitizedVersion);
this.dandiset = data;
} catch (err) {
if (axios.isAxiosError(err) && err.response && err.response.status < 500) {
this.dandiset = null;

if (isUnauthenticatedOrForbidden(err)) {
this.meta.dandisetExistsAndEmbargoed = true;
}

} else {
throw err;
}
Expand Down
15 changes: 3 additions & 12 deletions web/src/views/DandisetLandingView/DandisetLandingView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -193,20 +193,11 @@ watch(() => props.identifier, async () => {
}
// Set default values
embargoedOrUnauthenticated.value = false;
loading.value = true;
// Catch error to check if response is 401, for embargoed dandisets
try {
await store.initializeDandisets({ identifier, version });
} catch (e) {
if (axios.isAxiosError(e) && (e.response?.status === 401 || e.response?.status === 403)) {
embargoedOrUnauthenticated.value = true
} else {
throw e
}
}
// Check if response is 401 or 403, for embargoed dandisets
await store.initializeDandisets({ identifier, version });
embargoedOrUnauthenticated.value = store.meta.dandisetExistsAndEmbargoed;
loading.value = false;
}, { immediate: true });
Expand Down

0 comments on commit a317f91

Please sign in to comment.