Skip to content

Commit

Permalink
added try catch to handle not found
Browse files Browse the repository at this point in the history
  • Loading branch information
itzomen committed Aug 30, 2023
1 parent b1ebcaf commit e84c1ad
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 43 deletions.
102 changes: 60 additions & 42 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,26 @@ export class CMSClient {
};
}
} else {
const response = (await this.fetchContent(
`${contentType}/${idOrSlug}`,
queries,
headers,
cache
)) as CMSContent;

if (response.id && response.title) {
return response;
} else {
return {
message: "Page not found",
data: response,
};
try {
return (await this.fetchContent(
`${contentType}/${idOrSlug}`,
queries,
headers,
cache
)) as CMSContent;
} catch (error) {
if (error instanceof FetchError) {
return {
message: "Page not found",
data: error,
};
} else {
console.error("An unknown error occurred:", error);
return {
message: "An unknown error occurred:",
data: error,
};
}
}
}
}
Expand All @@ -143,20 +149,26 @@ export class CMSClient {
cache?: RequestCache
): Promise<CMSContent | NotFoundContents> {
const contentType = "images";
const response = (await this.fetchContent(
`${contentType}/${id}`,
queries,
headers,
cache
)) as CMSContent;

if (response.id && response.title) {
return response;
} else {
return {
message: "Image not found",
data: response,
};
try {
return (await this.fetchContent(
`${contentType}/${id}`,
queries,
headers,
cache
)) as CMSContent;
} catch (error) {
if (error instanceof FetchError) {
return {
message: "Image not found",
data: error,
};
} else {
console.error("An unknown error occurred:", error);
return {
message: "An unknown error occurred:",
data: error,
};
}
}
}

Expand All @@ -177,20 +189,26 @@ export class CMSClient {
cache?: RequestCache
): Promise<CMSContent | NotFoundContents> {
const contentType = "documents";
const response = (await this.fetchContent(
`${contentType}/${id}`,
queries,
headers,
cache
)) as CMSContent;

if (response.id && response.title) {
return response;
} else {
return {
message: "Document not found",
data: response,
};
try {
return (await this.fetchContent(
`${contentType}/${id}`,
queries,
headers,
cache
)) as CMSContent;
} catch (error) {
if (error instanceof FetchError) {
return {
message: "Document not found",
data: error,
};
} else {
console.error("An unknown error occurred:", error);
return {
message: "An unknown error occurred:",
data: error,
};
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { FetchError } from "..";

export interface ClientOptions {
baseURL: string;
apiPath: string;
Expand Down Expand Up @@ -60,5 +62,5 @@ export interface CMSContents {

export interface NotFoundContents {
message: string;
data: CMSContent| CMSContents;
data: CMSContent | CMSContents | FetchError | unknown;
}

0 comments on commit e84c1ad

Please sign in to comment.