From e84c1ad5f9fd752b50430327af5d63c846d288a4 Mon Sep 17 00:00:00 2001 From: Peng Boris Date: Wed, 30 Aug 2023 11:20:18 +0100 Subject: [PATCH] added try catch to handle not found --- src/index.ts | 102 +++++++++++++++++++++++++------------------ src/types/index.d.ts | 4 +- 2 files changed, 63 insertions(+), 43 deletions(-) diff --git a/src/index.ts b/src/index.ts index e61c22c..3a30d69 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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, + }; + } } } } @@ -143,20 +149,26 @@ export class CMSClient { cache?: RequestCache ): Promise { 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, + }; + } } } @@ -177,20 +189,26 @@ export class CMSClient { cache?: RequestCache ): Promise { 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, + }; + } } } diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 5c33e1f..db06158 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -1,3 +1,5 @@ +import { FetchError } from ".."; + export interface ClientOptions { baseURL: string; apiPath: string; @@ -60,5 +62,5 @@ export interface CMSContents { export interface NotFoundContents { message: string; - data: CMSContent| CMSContents; + data: CMSContent | CMSContents | FetchError | unknown; }