Skip to content

Commit

Permalink
refactor: split error trace from message
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jun 20, 2022
1 parent 155afac commit dee7308
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/ipx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export function createIPX (userOptions: Partial<IPXOptions>): IPX {
const getSrc = cachedPromise(() => {
const source = hasProtocol(id) ? 'http' : 'filesystem'
if (!ctx.sources[source]) {
throw createError('Unknown source: ' + source, 400)
throw createError('Unknown source', 400, source)
}
return ctx.sources[source](id, reqOptions)
})
Expand Down
8 changes: 4 additions & 4 deletions src/sources/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ export const createFilesystemSource: SourceFactory<FilesystemSourceOptions> = (o
return async (id: string) => {
const fsPath = resolve(join(rootDir, id))
if (!isValidPath(fsPath) || !fsPath.startsWith(rootDir)) {
throw createError('Forbidden path:' + id, 403)
throw createError('Forbidden path', 403, id)
}

let stats: Stats
try {
stats = await fsp.stat(fsPath)
} catch (err) {
if (err.code === 'ENOENT') {
throw createError('File not found: ' + fsPath, 404)
throw createError('File not found', 404, fsPath)
} else {
throw createError('File access error for ' + fsPath + ':' + err.code, 403)
throw createError('File access error ' + err.code, 403, fsPath)
}
}
if (!stats.isFile()) {
throw createError('Path should be a file: ' + fsPath, 400)
throw createError('Path should be a file', 400, fsPath)
}

return {
Expand Down
6 changes: 3 additions & 3 deletions src/sources/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export const createHTTPSource: SourceFactory<HTTPSourceOptions> = (options) => {

// Check host
if (!url.hostname) {
throw createError('Hostname is missing: ' + id, 403)
throw createError('Hostname is missing', 403, id)
}
if (!reqOptions?.bypassDomain && !hosts.find(host => url.hostname === host)) {
throw createError('Forbidden host: ' + url.hostname, 403)
throw createError('Forbidden host', 403, url.hostname)
}

const response = await fetch(id, {
Expand All @@ -41,7 +41,7 @@ export const createHTTPSource: SourceFactory<HTTPSourceOptions> = (options) => {
})

if (!response.ok) {
throw createError(response.statusText || 'fetch error', response.status || 500)
throw createError('Fetch error', response.status || 500, response.statusText)
}

let maxAge = options.maxAge
Expand Down
7 changes: 4 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import destr from 'destr'
import xss from 'xss'

export function getEnv (name: string, defaultValue: any) {
return destr(process.env[name]) ?? defaultValue
Expand All @@ -20,9 +21,9 @@ export class IPXError extends Error {
statusMessage?: string
}

export function createError (message: string, statusCode: number): IPXError {
const err = new IPXError(message)
err.statusMessage = 'IPX: ' + message
export function createError (statusMessage: string, statusCode: number, trace?: string): IPXError {
const err = new IPXError(statusMessage + (trace ? ` (${trace})` : ''))
err.statusMessage = 'IPX: ' + statusMessage
err.statusCode = statusCode
return err
}

0 comments on commit dee7308

Please sign in to comment.