Skip to content

Commit

Permalink
refactor: improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Sep 27, 2023
1 parent da27218 commit 9826cda
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion playground.ts
Expand Up @@ -6,7 +6,7 @@ const ipx = createIPX({
"/picsum": "https://picsum.photos",
},
httpStorage: ipxHttpStorage({
domains: ["picsum.photos"],
domains: ["picsum.photos", "images.unsplash.com"],
}),
});

Expand Down
15 changes: 14 additions & 1 deletion src/ipx.ts
Expand Up @@ -73,6 +73,7 @@ export function createIPX(userOptions: IPXOptions): IPX {
if (!id) {
throw createError({
statusCode: 400,
statusText: `IPX_MISSING_ID`,
message: `Resource id is missing`,
});
}
Expand All @@ -94,6 +95,7 @@ export function createIPX(userOptions: IPXOptions): IPX {
if (!storage) {
throw createError({
statusCode: 500,
statusText: `IPX_NO_STORAGE`,
message: "No storage configured!",
});
}
Expand All @@ -104,6 +106,7 @@ export function createIPX(userOptions: IPXOptions): IPX {
if (!sourceMeta) {
throw createError({
statusCode: 404,
statusText: `IPX_RESOURCE_NOT_FOUND`,
message: `Resource not found: ${id}`,
});
}
Expand All @@ -120,6 +123,7 @@ export function createIPX(userOptions: IPXOptions): IPX {
if (!sourceData) {
throw createError({
statusCode: 404,
statusText: `IPX_RESOURCE_NOT_FOUND`,
message: `Resource not found: ${id}`,
});
}
Expand All @@ -131,7 +135,16 @@ export function createIPX(userOptions: IPXOptions): IPX {
const sourceData = await getSourceData();

// Detect source image meta
const imageMeta = getImageMeta(sourceData) as ImageMeta;
let imageMeta: ImageMeta;
try {
imageMeta = getImageMeta(sourceData) as ImageMeta;
} catch {
throw createError({
statusCode: 400,
statusText: `IPX_INVALID_IMAGE`,
message: `Cannot parse image metadata: ${id}`,
});
}

// Determine format
let mFormat = modifiers.f || modifiers.format;
Expand Down
10 changes: 8 additions & 2 deletions src/server.ts
Expand Up @@ -32,12 +32,14 @@ export function createIPXH3Handler(ipx: IPX) {
if (!modifiersString) {
throw createError({
statusCode: 400,
statusText: `IPX_MISSING_MODIFIERS`,
message: `Modifiers are missing: ${id}`,
});
}
if (!id || id === "/") {
throw createError({
statusCode: 400,
statusText: `IPX_MISSING_ID`,
message: `Resource id is missing: ${event.path}`,
});
}
Expand Down Expand Up @@ -124,9 +126,13 @@ export function createIPXH3Handler(ipx: IPX) {
return await _handler(event);
} catch (_error: unknown) {
const error = createError(_error as H3Error);
setResponseStatus(event, error.statusCode, "IPXError");
setResponseStatus(event, error.statusCode, error.statusMessage);
return {
error: "[IPXError] " + error.message,
error: {
message: `[${error.statusCode}] [${
error.statusMessage || "IPX_ERROR"
}] ${error.message}`,
},
};
}
});
Expand Down
5 changes: 4 additions & 1 deletion src/storage/http.ts
Expand Up @@ -42,12 +42,14 @@ export function ipxHttpStorage(_options: HTTPStorageOptions = {}): IPXStorage {
if (!url.hostname) {
throw createError({
statusCode: 403,
statusText: `IPX_MISSING_HOSTNAME`,
message: `Hostname is missing: ${id}`,
});
}
if (!allowAllDomains && !domains.has(url.hostname)) {
throw createError({
statusCode: 403,
statusText: `IPX_FORBIDDEN_HOST`,
message: `Forbidden host: ${url.hostname}`,
});
}
Expand All @@ -59,7 +61,8 @@ export function ipxHttpStorage(_options: HTTPStorageOptions = {}): IPXStorage {
if (!response.ok) {
throw createError({
statusCode: response.status || 500,
message: `Fetch error: ${response.statusText}`,
statusText: response.statusText || `IPX_FETCH_ERROR`,
message: `Fetch error: [${response.status}] [${response.statusText}]`,
});
}

Expand Down
6 changes: 5 additions & 1 deletion src/storage/node-fs.ts
Expand Up @@ -18,6 +18,7 @@ export function ipxFSStorage(_options: NodeFSSOptions = {}): IPXStorage {
if (!isValidPath(resolved) || !resolved.startsWith(rootDir)) {
throw createError({
statusCode: 403,
statusText: `IPX_FORBIDDEN_PATH`,
message: `Forbidden path: ${id}`,
});
}
Expand All @@ -39,16 +40,19 @@ export function ipxFSStorage(_options: NodeFSSOptions = {}): IPXStorage {
throw error.code === "ENOENT"
? createError({
statusCode: 404,
statusText: `IPX_FILE_NOT_FOUND`,
message: `File not found: ${id}`,
})
: createError({
statusCode: 403,
message: `File access error: (${error.code}) ${id}`,
statusText: `IPX_FORBIDDEN_FILE`,
message: `File access forbidden: (${error.code}) ${id}`,
});
}
if (!stats.isFile()) {
throw createError({
statusCode: 400,
statusText: `IPX_INVALID_FILE`,
message: `Path should be a file: ${id}`,
});
}
Expand Down

0 comments on commit 9826cda

Please sign in to comment.