Skip to content

Commit

Permalink
Fix error handling for win
Browse files Browse the repository at this point in the history
  • Loading branch information
gmaclennan committed Oct 17, 2024
1 parent 753a3aa commit ba3e2d7
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default function (fastify, { filepath }, done) {
const baseUrl = new URL(fastify.prefix, fastify.listeningOrigin)
return reader.getStyle(baseUrl.href)
} catch (error) {
if (isENOENT(error)) {
if (isFileNotThereError(error)) {
throw createError(404, error.message)
}
console.error(error)
Expand All @@ -72,7 +72,7 @@ export default function (fastify, { filepath }, done) {
const resource = await reader.getResource(path)
return sendResource(reply, resource)
} catch (error) {
if (isENOENT(error)) {
if (isFileNotThereError(error)) {
throw createError(404, error.message)
}
console.error(error)
Expand All @@ -82,14 +82,6 @@ export default function (fastify, { filepath }, done) {
done()
}

/**
* @param {unknown} error
* @returns {error is Error & { code: 'ENOENT' }}
*/
function isENOENT(error) {
return error instanceof Error && 'code' in error && error.code === 'ENOENT'
}

class DeferredReader {
/** @type {Reader | undefined} */
#reader
Expand Down Expand Up @@ -132,11 +124,7 @@ class DeferredReader {
console.log('Error event watching file:', error)
})
} catch (error) {
if (
error instanceof Error &&
'code' in error &&
(error.code === 'ENOENT' || error.code === 'EPERM')
) {
if (isFileNotThereError(error)) {
// Ignore: File does not exist yet, but we'll try to open it later
} else {
throw error
Expand Down Expand Up @@ -202,3 +190,15 @@ class DeferredReader {
function isWin() {
return process.platform === 'win32'
}

/**
* @param {unknown} error
* @returns {error is Error & { code: 'ENOENT' | 'EPERM' }}
*/
function isFileNotThereError(error) {
return (
error instanceof Error &&
'code' in error &&
(error.code === 'ENOENT' || error.code === 'EPERM')
)
}

0 comments on commit ba3e2d7

Please sign in to comment.