A package to handle errors as a result and not throw errors
# npm
npm install @dancastillo/nothrow
# yarn
yarn install @dancastillo/nothrow
# pnpm
pnpm install @dancastillo/nothrow
import { Result } from '@dancastillo/nothrow'
Constructs a successful result with no errors
const result = Result.successful({ success: true })
// result.data = { success: true }
// result.errors = undefined
Constructs a failed result with no data
const result = Result.failure({ success: false })
// result.data = undefined
// result.errors = { success: false }
Constructs a result with data and errors
const result = Result.partialSuccess({ success: true }, [{ error: 'Missing input' }, { error: 'Not found' }])
// result.data ={ success: true }
// result.errors = [{ error: 'Missing input' }, { error: 'Not found' }]
Map the data result and errors result to the wanted type. This method takes in two functions as its arguments
const mappedResult = result.map(
(data: DataType) => {
return <MappedDataType>{ id: data.id }
},
(errors: ErrorType) => {
return <MappedErrorType[]>errors.map((err) => {
error: err.message
})
}
)
Map the data result and errors result to the wanted type. This method takes in two functions as its arguments
const mappedResult = result.mapData((data: DataType) => {
return <MappedDataType>{ id: data.id }
})
Map the data result and errors result to the wanted type. This method takes in two functions as its arguments
const mappedResult = result.mapErrors((errors: ErrorType) => {
return <MappedErrorType[]>errors.map((err) => {
error: err.message
})
})
Returns TRUE
if the result was successful and no errors
Returns TRUE
if the result was N error reults and no data
Returns FALSE
if the result returned both a data result and error result
Helper method to determine if the Result
has data value set
Helper method to retrieve the Result
data value set
Helper method to determine if the Result
has error values set
Helper method to retrieve the Result
error values set
Helper method to retrieve the Result
error values count