Skip to content

Commit

Permalink
feat: 🎸 add castError
Browse files Browse the repository at this point in the history
  • Loading branch information
l246804 committed Aug 15, 2023
1 parent 7bf2fea commit f60bc1e
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/castError/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { isError } from 'lodash-unified'

/**
* 强制转换为 `Error`
*
* @example
* ```ts
* castError(1)
* // => Error { error: 'error', message: '1' }
*
* castError(new Error('This is error'))
* // => Error { name: 'error', message: 'This is error' }
*
* castError({ name: 'TypedError', message: 'This is TypedError' })
* // /=> Object { name: 'TypedError', message: 'This is TypedError' }
* ```
*/
export default function castError(value) {
return isError(value) ? value : new Error(String(value))
}

0 comments on commit f60bc1e

Please sign in to comment.