![38% of CISOs Fear They’re Not Moving Fast Enough on AI](https://cdn.sanity.io/images/cgdhsj6q/production/faa0bc28df98f791e11263f8239b34207f84b86f-1024x1024.webp?w=400&fit=max&auto=format)
Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Handle errors in a better way.
# install with npm
npm add goerr
# install with yarn
yarn add goerr
# install with pnpm
pnpm add goerr
// ES Module
import goerr from 'goerr'
// CommonJS
const goerr = require('goerr').default
goerr
method expects a function (especially a lambda expression) as the argument, in which calling some throwable functions and returning a value.
The value will be the first element of an array returned by goerr
. Any error throwed when calling the function will be the second element of the array.
Note: You cannot return a Promise
object in the throwableFunction
. If you want to handle asynchronous errors, see the next section.
function goerr<T>(func: () => T): [T, Error]
const [result, err] = goerr(() => throwableFunction())
if (err) {
// do something here
}
// go ahead with the result
console.log(result)
goerr
can accept a Promise
object in order to handle cases you have to do asynchronously.
function goerr<T>(promise: Promise<T>): Promise<[T, Error]>
const [result, err] = await goerr(axios.get('https://example.com'))
if (err) {
// do something here
}
// go ahead with the result
console.log(result)
goerr
can accept an asynchronous function in which you could use async/await
.
function goerr<T>(asyncFunc: () => Promise<T>): Promise<[T, Error]>
const [result, err] = await goerr(async () => {
const num1 = await calculateAsync()
const num2 = await calculateAsync()
return num1 + num2
})
if (err) {
// do something here
}
// go ahead with the result
console.log(result)
Since we all know that try
/catch
works good in JavaScript, you might be confused about if we really need goerr
to handle errors.
Suppose now we are writing a function to return a list coming from a throwable function, and return an empty list when an error throwed out.
How could we do?
function listOf() {
try {
const list = throwable()
return list
} catch (_) {
return []
}
}
It looks good, but what if we need to do some operations on this list?
function listOf() {
try {
const list = throwable()
for (let item of list) {
// do something with items
}
return list
} catch (_) {
return []
}
}
Good job! But if we have a better choise?
The defect of solution 2 is about code smell. We should return as soon as we can to avoid nested code.
function listOf() {
const [list, err] = goerr(throwable)
if (err) {
return []
}
for (let item of list) {
// do something with items
}
return list
}
Now you see the for
loop comes to the top level of the function. It looks more clear, right?
FAQs
handle errors in a better way.
We found that goerr demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.