Skip to content

Commit

Permalink
show 404 when tenantId not found
Browse files Browse the repository at this point in the history
  • Loading branch information
scottaubrey committed Nov 5, 2024
1 parent 28ad627 commit 3fe89b3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/pages/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { GetServerSideProps, GetServerSidePropsContext } from 'next';
import { fetchTenantUsingContext, fetchVersions } from '../utils/data-fetch';
import { ArticleSummary } from '../types';
import { Heading } from '../components/atoms/heading/heading';
import { HasTenant } from '../tenant';
import { HasTenant, TenantData } from '../tenant';

type PageProps = HasTenant & {
ids?: string[],
Expand Down Expand Up @@ -35,7 +35,12 @@ export const App = ({ ids, articles, previews }: PageProps) => (
);

export const getServerSideProps: GetServerSideProps<PageProps> = async (context: GetServerSidePropsContext) => {
const tenant = await fetchTenantUsingContext(context);
let tenant: TenantData;
try {
tenant = await fetchTenantUsingContext(context);
} catch (e) {
return { notFound: true };
}

const versions = (await fetchVersions(tenant.id)).items.sort((a, b) => (a.id > b.id ? 1 : -1));
const articles = versions.filter((version) => (version.date));
Expand Down
9 changes: 7 additions & 2 deletions src/pages/reviewed-preprints/[...path].page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { ErrorMessages } from '../../components/atoms/error-messages/error-messa
import { formatAuthorName } from '../../utils/formatters';
import { makeNullableOptional } from '../../utils/make-nullable-optional';
import { SerialisedTimelineEvent } from '../../types/article-timeline';
import { HasTenant } from '../../tenant';
import { HasTenant, TenantData } from '../../tenant';

type PageProps = HasTenant & {
metaData: MetaData,
Expand Down Expand Up @@ -179,7 +179,12 @@ export const getServerSideProps: GetServerSideProps<PageProps> = async (context:
return { notFound: true };
}

const tenant = await fetchTenantUsingContext(context);
let tenant: TenantData;
try {
tenant = await fetchTenantUsingContext(context);
} catch (e) {
return { notFound: true };
}

const idParts = [...context.params?.path as string[]];

Expand Down
2 changes: 1 addition & 1 deletion src/utils/data-fetch/fetch-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ArticleSummary, EnhancedArticleWithVersions } from '../../types';
import { EnhancedArticleNoContent } from '../../types/reviewed-preprint-snippet';
import { TenantData } from '../../tenant';

export const fetchTenant = (tenantId: string) => jsonFetch<TenantData>(`${config.apiServer}/api/${tenantId}/config`);
export const fetchTenant = (tenantId: string) => jsonFetchOrNull<TenantData>(`${config.apiServer}/api/${tenantId}/config`);
export const fetchVersion = (tenantId: string, id: string, preview: boolean = false) => jsonFetchOrNull<EnhancedArticleWithVersions>(`${config.apiServer}/api/${tenantId}/preprints/${id}${preview ? '?previews=true' : ''}`);
export const fetchVersions = (tenantId: string) => jsonFetch<{ items: ArticleSummary[], total: number }>(`${config.apiServer}/api/${tenantId}/preprints`);
export const fetchVersionsNoContent = async (tenantId: string, page: number, perPage: number, order: 'asc' | 'desc', useDate: 'default' | 'published', startDate: string, endDate: string) => {
Expand Down

0 comments on commit 3fe89b3

Please sign in to comment.