Files
speckle-server/packages/server/modules/core/graph/setup.js
T
Kristaps Fabians Geikins 79d293e178 fix: preparing automation backend for use in FE (#1790)
* fix: preparing automation backend for use in FE

* minor migration adjustment

* modelId only

* more CR fixes

* improved validation

* WIP model automation status (#1791)

* additional run status insert validations

* model card dialog

* show view automation btn

* story mock fixes

* version card dialog

---------

Co-authored-by: Gergő Jedlicska <gergo@jedlicska.com>
2023-09-18 11:49:11 +03:00

69 lines
1.9 KiB
JavaScript

const _ = require('lodash')
const VError = require('verror')
const { ZodError } = require('zod')
const { fromZodError } = require('zod-validation-error')
/**
* Some VError implementation details that we want to remove from object representations
* of VErrors once they're converted to them
*/
const VERROR_TRASH_PROPS = ['jse_shortmsg', 'jse_cause', 'jse_info']
/**
* Builds apollo server error formatter
* @param {boolean} debug
* @returns {(e: import('graphql').GraphQLError) => import('graphql').GraphQLFormattedError}
*/
function buildErrorFormatter(debug) {
// TODO: Add support for client-aware errors and obfuscate everything else
return function (error) {
const debugMode = debug
const realError = error.originalError ? error.originalError : error
// If error is a ZodError, convert its message to something more readable
if (realError instanceof ZodError) {
return {
...error,
message: fromZodError(realError).message,
extensions: { ...error.extensions, code: 'BAD_REQUEST' }
}
}
// If error isn't a VError child, don't do anything extra
if (!(realError instanceof VError)) {
return error
}
// Converting VError based error to Apollo's format
const extensions = {
...(error.extensions || {}),
...(VError.info(realError) || {})
}
// Getting rid of redundant info
delete extensions.originalError
// Updating exception metadata in extensions
if (extensions.exception) {
extensions.exception = _.omit(extensions.exception, VERROR_TRASH_PROPS)
if (debugMode) {
extensions.exception.stacktrace = VError.fullStack(realError)
} else {
delete extensions.exception.stacktrace
}
}
return {
message: error.message,
locations: error.locations,
path: error.path,
extensions
}
}
}
module.exports = {
buildErrorFormatter
}