Skip to content

Commit

Permalink
Created custom error class for error handling when no data
Browse files Browse the repository at this point in the history
  • Loading branch information
nroh555 committed Jun 30, 2024
1 parent 5fd8208 commit 2c29726
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 80 deletions.
6 changes: 6 additions & 0 deletions web/src/classes/NoDataError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class NoDataError extends Error {
constructor(message: string) {
super(message);
this.name = "NoDataError";
}
}
161 changes: 99 additions & 62 deletions web/src/utils/Mapper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { NoDataError } from "../classes/NoDataError";
import type {
Exec,
Partner,
Expand All @@ -11,86 +12,122 @@ import type {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export class Mapper {
static mapToExec(data: any): Exec[] {
return data.execs.data.map((item: any) => {
const attributes = item.attributes || {};
const imageUrl = attributes.Image?.data?.attributes?.url || "";
if (!data.execs || !data.execs.data || data.execs.data.length === 0) {
throw new NoDataError("No data");
} else {
return data.execs.data.map((item: any) => {
const attributes = item.attributes || {};
const imageUrl = attributes.Image?.data?.attributes?.url || "";

return {
id: item.id,
name: attributes.Name || "",
description: attributes.Description || "",
position: attributes.Position || "",
role: attributes.Role || "",
image: imageUrl,
};
});
return {
id: item.id,
name: attributes.Name || "",
description: attributes.Description || "",
position: attributes.Position || "",
role: attributes.Role || "",
image: imageUrl,
};
});
}
}

static mapToPartner(data: any): Partner[] {
return data.partners.data.map((item: any) => {
const attributes = item.attributes || {};
const imageUrl = attributes.Image?.data?.attributes?.url || "";
if (
!data.partners ||
!data.partners.data ||
data.partners.data.length === 0
) {
throw new NoDataError("No data");
} else {
return data.partners.data.map((item: any) => {
const attributes = item.attributes || {};
const imageUrl = attributes.Image?.data?.attributes?.url || "";

return {
id: item.id,
type: attributes.Type || "",
name: attributes.Name || "",
description: attributes.Description || "",
location: attributes.Location || "",
image: imageUrl,
};
});
return {
id: item.id,
type: attributes.Type || "",
name: attributes.Name || "",
description: attributes.Description || "",
location: attributes.Location || "",
image: imageUrl,
};
});
}
}

static mapToSocials(data: any): Social[] {
return data.socials.data.map((item: any) => {
const attributes = item.attributes || {};
return {
id: item.id,
type: attributes.Type || "",
link: attributes.Link || "",
};
});
if (!data.socials || !data.socials.data || data.socials.data.length === 0) {
throw new NoDataError("No data");
} else {
return data.socials.data.map((item: any) => {
const attributes = item.attributes || {};
return {
id: item.id,
type: attributes.Type || "",
link: attributes.Link || "",
};
});
}
}

static mapToSomePhotos(data: any): SomePhoto[] {
return data.somePhotos.data.map((item: any) => {
const attributes = item.attributes || {};
const imageUrl = attributes.Image?.data?.attributes?.url || "";
if (
!data.somePhotos ||
!data.somePhotos.data ||
data.somePhotos.data.length === 0
) {
throw new NoDataError("No data");
} else {
return data.somePhotos.data.map((item: any) => {
const attributes = item.attributes || {};
const imageUrl = attributes.Image?.data?.attributes?.url || "";

return {
id: item.id,
title: attributes.Title || "",
year: attributes.Year || "",
image: imageUrl,
};
});
return {
id: item.id,
title: attributes.Title || "",
year: attributes.Year || "",
image: imageUrl,
};
});
}
}

static mapToValue(data: any): Value[] {
return data.values.data.map((item: any) => {
const attributes = item.attributes || {};
const imageUrl = attributes.Image?.data?.attributes?.url || "";
if (!data.values || !data.values.data || data.values.data.length === 0) {
throw new NoDataError("No data");
} else {
return data.values.data.map((item: any) => {
const attributes = item.attributes || {};
const imageUrl = attributes.Image?.data?.attributes?.url || "";

return {
id: item.id,
title: attributes.Title || "",
description: attributes.Description || "",
image: imageUrl,
};
});
return {
id: item.id,
title: attributes.Title || "",
description: attributes.Description || "",
image: imageUrl,
};
});
}
}

static mapToIntroduction = (data: any): Introduction[] => {
return data.introductions.data.map((item: any) => {
const attributes = item.attributes || {};
return {
id: item.id,
description: attributes.Description || "",
events: attributes.Events || "",
members: attributes.Members || "",
followers: attributes.Followers || "",
};
});
if (
!data.introductions ||
!data.introductions.data ||
data.introductions.data.length === 0
) {
throw new NoDataError("No data");
} else {
return data.introductions.data.map((item: any) => {
const attributes = item.attributes || {};
return {
id: item.id,
description: attributes.Description || "",
events: attributes.Events || "",
members: attributes.Members || "",
followers: attributes.Followers || "",
};
});
}
};
}
18 changes: 0 additions & 18 deletions web/src/utils/mapToValue.ts

This file was deleted.

0 comments on commit 2c29726

Please sign in to comment.