Skip to content

Commit

Permalink
feat: log plugin name on error
Browse files Browse the repository at this point in the history
closes #28
  • Loading branch information
antongolub committed Nov 9, 2022
1 parent 7b2e334 commit a2347ff
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
11 changes: 9 additions & 2 deletions src/main/js/firewall/engine.js
@@ -1,4 +1,5 @@
import {asArray, mapValuesAsync} from '../util.js'
import {logger} from '../logger.js'

export const getDirectives = ({packument, rules, boundContext}) =>
mapValuesAsync(packument.versions, async (entry) =>
Expand All @@ -21,8 +22,14 @@ export const getDirective = async ({rules, entry, boundContext}) => {
return _m
}

const policy = await plugin({rule, entry, options, boundContext})
return policy ? {...rule, policy} : false
try {
const policy = await plugin({rule, entry, options, boundContext})
return policy ? {...rule, policy} : false
} catch (e){
logger.error(`Error in plugin ${plugin.name}`, e)
return false
}

}, false)
}

Expand Down
8 changes: 7 additions & 1 deletion src/main/js/firewall/middleware.js
Expand Up @@ -7,7 +7,13 @@ import {getCtx} from '../als.js'
import {checkTarball} from './tarball.js'
import {logger} from '../logger.js'

const warmupPipeline = (pipeline, opts) => pipeline.forEach(([plugin, _opts]) => plugin.warmup?.({...opts, ..._opts }))
const warmupPipeline = (pipeline, opts) => pipeline.forEach(([plugin, _opts]) => {
try {
plugin.warmup?.({...opts, ..._opts })
} catch (e) {
logger.error(`Error in plugin ${plugin.name} warmup`, e)
}
})

const warmupDepPackuments = (name, deps, boundContext, rules) => {
if (isNoCache()) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/js/firewall/plugins/audit.js
@@ -1,7 +1,7 @@
import {semver} from '../../semver.js'
import {request} from '../../http/index.js'
import {getCache, withCache} from '../../cache.js'
import {asArray, makeDeferred, tryQueue} from '../../util.js'
import {asArray, makeDeferred, setFnName, tryQueue} from '../../util.js'
import {logger} from '../../logger.js'

const severityOrder = ['critical', 'high', 'moderate', 'low', 'any' ]
Expand All @@ -21,6 +21,8 @@ auditPlugin.warmup = ({name, registry}) => {
return getAdvisories(name, registry)
}

setFnName(auditPlugin, 'audit-plugin')

const getAdvisories = async (name, registry) => {
const registries = asArray(registry)
const args = registries.map(r => [name, r])
Expand Down
4 changes: 3 additions & 1 deletion src/main/js/firewall/plugins/std.js
@@ -1,5 +1,5 @@
import {semver} from '../../semver.js'
import {asRegExp} from '../../util.js'
import {asRegExp, setFnName} from '../../util.js'

export const stdPlugin = async ({rule, entry, boundContext = {}, options = rule}) => {
const filter = options.filter || defaultFilter
Expand All @@ -11,6 +11,8 @@ export const stdPlugin = async ({rule, entry, boundContext = {}, options = rule}
return !!matched && options.policy
}

setFnName(stdPlugin, 'std-plugin')

export const defaultFilter = ({options: opts, boundContext: {org}, entry: { name, version, time, license, _npmUser }, now = Date.now()}) => {
const matches = [
matchByName(opts, name, version),
Expand Down
2 changes: 2 additions & 0 deletions src/main/js/util.js
Expand Up @@ -196,3 +196,5 @@ export const time = (fn, label = fn.name) => async (...args) => {

return fn(...args).finally(() => logger.debug(`${label} took ${Date.now() - b}ms`))
}

export const setFnName = (fn, name) => Object.defineProperty(fn, 'name', { value: name })

0 comments on commit a2347ff

Please sign in to comment.