Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
feat(utils): ignore404
Browse files Browse the repository at this point in the history
  • Loading branch information
unicornware committed Oct 9, 2021
1 parent 7dc928f commit acee839
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = {
skipWords: [
...RULES_SPELLCHECKER[1].skipWords,
'callee',
'errno',
'trext',
'trextel'
]
Expand Down
Empty file removed __tests__/__fixtures__/.gitkeep
Empty file.
10 changes: 10 additions & 0 deletions __tests__/__fixtures__/errno-exception.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @file Test Fixture - ErrnoException
* @module tests/fixtures/ErrnoException
*/

const EXCEPTION: NodeJS.ErrnoException = new Error('Test errno message')

EXCEPTION.code = 'ERRNO'

export default EXCEPTION
6 changes: 6 additions & 0 deletions __tests__/__fixtures__/error.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @file Test Fixture - Error
* @module tests/fixtures/Error
*/

export default new Error('Test error message')
48 changes: 48 additions & 0 deletions src/utils/__tests__/ignore-404.util.functional.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import ERRNO_EXCEPTION from '@tests/fixtures/errno-exception.fixture'
import ERROR from '@tests/fixtures/error.fixture'
import type { Testcase } from '@tests/utils/types'
import testSubject from '../ignore-404.util'

/**
* @file Functional Tests - ignore404
* @module trext/utils/tests/functional/ignore404
*/

describe('functional:utils/ignore404', () => {
type Case = Testcase<string | undefined> & {
error: Error | NodeJS.ErrnoException
throw: 'throw' | 'not throw'
state: string
}

const cases: Case[] = [
{
error: ERROR,
expected: undefined,
state: 'promise contains Error',
throw: 'not throw'
},
{
error: ERRNO_EXCEPTION,
expected: ERRNO_EXCEPTION.code,
state: 'promise contains NodeJS.ErrnoException',
throw: 'throw'
}
]

it.each<Case>(cases)('should $throw if $state', async testcase => {
// Arrange
const { error, expected } = testcase
let exception: NodeJS.ErrnoException | null = null

// Act
try {
await testSubject(new Promise((resolve, reject) => reject(error)))
} catch (error) {
exception = error as NodeJS.ErrnoException
}

// Expect
expect(exception?.code).toBe(expected)
})
})
27 changes: 27 additions & 0 deletions src/utils/ignore-404.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @file Utilities - ignore404
* @module trext/utils/ignore404
*/

/**
* Waits for a promise to complete and rejects **only if** an error is thrown
* and the error is **not** a missing file or directory error.
*
* @template T - Data in promise
*
* @async
* @param {Promise<T>} p - Promise to evaluate
* @return {Promise<T | null>} Data in promise or null
* @throws {NodeJS.ErrnoException}
*/
async function ignore404<T extends any>(p: Promise<T>): Promise<T | null> {
try {
return await p
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error
}

return null
}

export default ignore404
6 changes: 6 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @file Entry Point - Utilities
* @module trext/utils
*/

export { default as ignore404 } from './ignore-404.util'

0 comments on commit acee839

Please sign in to comment.