Skip to content

Commit

Permalink
Merge pull request #220 from smartprocure/feature/errors-bubbling
Browse files Browse the repository at this point in the history
Let original errors bubble up when running searches
  • Loading branch information
stellarhoof authored Jun 12, 2024
2 parents 1edec5f + 11186dc commit d8ca416
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 61 deletions.
6 changes: 6 additions & 0 deletions .changeset/nine-donkeys-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'contexture-elasticsearch': patch
'contexture': patch
---

Bubble up original errors
24 changes: 7 additions & 17 deletions packages/provider-elasticsearch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,13 @@ let ElasticsearchProvider = (config = { request: {} }) => ({

let metaObj = { request, requestOptions }

try {
// Log Request
node._meta.requests.push(metaObj)
let count = counter.inc()
debug('(%s) Request: %O\nOptions: %O', count, request, requestOptions)
let { body } = await search(request, requestOptions)
metaObj.response = body
debug('(%s) Response: %O', count, body)
} catch (e) {
console.error({ e })
metaObj.response = e
node.error = e.meta.body.error
throw {
message: `${e}`,
...e.meta.body.error,
}
}
// Log Request
node._meta.requests.push(metaObj)
let count = counter.inc()
debug('(%s) Request: %O\nOptions: %O', count, request, requestOptions)
let { body } = await search(request, requestOptions)
metaObj.response = body
debug('(%s) Response: %O', count, body)

return metaObj.response
},
Expand Down
70 changes: 33 additions & 37 deletions packages/server/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,42 @@ let process = _.curry(async ({ providers, schemas }, group, options = {}) => {
getProvider,
processGroup: (g, options) => process({ providers, schemas }, g, options),
})
try {
await attachFilters(runTypeFunction)(group)
Tree.walk((node) => {
// Skip groups
if (!Tree.traverse(node))
node._meta.relevantFilters = getRelevantFilters(
getProvider(node).groupCombinator,
node._meta.path,
group
)
})(group)
await Tree.walkAsync(async (node) => {
let validContext = await runTypeFunction('validContext', node)

// Reject filterOnly
if (node.filterOnly || !validContext) {
if (!options.debug) delete node._meta
return
}
let curriedSearch = _.partial(getProvider(node).runSearch, [
options,
node,
getSchema(node.schema),
node._meta.relevantFilters,
])

node.context = await runTypeFunction('result', node, curriedSearch).catch(
(error) => {
throw F.extendOn(error, { node })
}
await attachFilters(runTypeFunction)(group)
Tree.walk((node) => {
// Skip groups
if (!Tree.traverse(node))
node._meta.relevantFilters = getRelevantFilters(
getProvider(node).groupCombinator,
node._meta.path,
group
)
let path = node._meta.path
})(group)
await Tree.walkAsync(async (node) => {
let validContext = await runTypeFunction('validContext', node)

// Reject filterOnly
if (node.filterOnly || !validContext) {
if (!options.debug) delete node._meta
if (options.onResult) options.onResult({ path, node })
})(group)
return
}
let curriedSearch = _.partial(getProvider(node).runSearch, [
options,
node,
getSchema(node.schema),
node._meta.relevantFilters,
])

node.context = await runTypeFunction('result', node, curriedSearch).catch(
(error) => {
throw F.extendOn(error, { node })
}
)
let path = node._meta.path
if (!options.debug) delete node._meta
if (options.onResult) options.onResult({ path, node })
})(group)

return group
} catch (error) {
throw error.node ? error : new Error(`Uncaught search exception: ${error}`)
}
return group
})

export default process
Expand Down
11 changes: 4 additions & 7 deletions packages/server/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,10 @@ export let runTypeFunction = (config) => async (name, node, search) => {
? fn(node, search, schema, config)
: fn(node, schema, config))
} catch (error) {
throw {
message: `Failed running search for ${node.type} (${
node.key
}) at ${name}: ${_.getOr(error, 'message', error)}`,
error,
node,
}
// Sometimes we throw strings instead of errors
const message = _.getOr(error, 'message', error)
error.message = `(at contexture node with key=${node.key} and type=${node.type}) ${message}`
throw error
}
}

Expand Down

0 comments on commit d8ca416

Please sign in to comment.