From dbef9f0a35fecc404db390f3508dc427b0dc992b Mon Sep 17 00:00:00 2001 From: hacksalot Date: Sat, 13 Feb 2016 00:11:52 -0500 Subject: [PATCH] Improve VALIDATE error handling. --- dist/cli/error.js | 4 ++++ dist/cli/msg.yml | 4 +++- dist/cli/out.js | 6 +++++- dist/core/status-codes.js | 3 ++- dist/verbs/validate.js | 34 +++++++++++++++++---------------- src/cli/error.coffee | 4 ++++ src/cli/msg.yml | 4 +++- src/cli/out.coffee | 3 ++- src/core/status-codes.coffee | 1 + src/verbs/validate.coffee | 37 +++++++++++++++++++----------------- 10 files changed, 62 insertions(+), 38 deletions(-) diff --git a/dist/cli/error.js b/dist/cli/error.js index 462c9d2..c7f6722 100644 --- a/dist/cli/error.js +++ b/dist/cli/error.js @@ -230,6 +230,10 @@ Error-handling routines for HackMyResume. case HMSTATUS.createError: msg = printf(M2C(this.msgs.createError.msg), ex.inner.path); etype = 'error'; + break; + case HMSTATUS.validateError: + msg = printf(M2C(this.msgs.validateError.msg), ex.inner.toString()); + etype = 'error'; } return { msg: msg, diff --git a/dist/cli/msg.yml b/dist/cli/msg.yml index 3161e3b..b60bdb2 100644 --- a/dist/cli/msg.yml +++ b/dist/cli/msg.yml @@ -44,7 +44,7 @@ events: - "INVALID" - "BROKEN" - "MISSING" - - "UNKNOWN" + - "ERROR" beforePeek: msg: - Peeking at **%s** in **%s** @@ -107,3 +107,5 @@ errors: msg: Failed to create **'%s'**. exiting: msg: Exiting with status code **%s**. + validateError: + msg: "An error occurred during validation:\n%s" diff --git a/dist/cli/out.js b/dist/cli/out.js index 9e1adae..43b7843 100644 --- a/dist/cli/out.js +++ b/dist/cli/out.js @@ -151,8 +151,12 @@ Output routines for HackMyResume. case 'missing': style = 'red'; adj = msgs[4]; + break; + case 'unknown': + style = 'red'; + adj = msgs[5]; } - evt.schema = evt.schema.toUpperCase(); + evt.schema = evt.schema.replace('jars', 'JSON Resume').toUpperCase(); L(M2C(msgs[0], 'white') + chalk[style].bold(adj), evt.file, evt.schema); if (evt.violations) { _.each(evt.violations, function(err, idx) { diff --git a/dist/core/status-codes.js b/dist/core/status-codes.js index 965b2ba..3a8fc4c 100644 --- a/dist/core/status-codes.js +++ b/dist/core/status-codes.js @@ -32,7 +32,8 @@ Status codes for HackMyResume. themeLoad: 22, invalidParamCount: 23, missingParam: 24, - createError: 25 + createError: 25, + validateError: 26 }; }).call(this); diff --git a/dist/verbs/validate.js b/dist/verbs/validate.js index 4a11eb9..02de2c8 100644 --- a/dist/verbs/validate.js +++ b/dist/verbs/validate.js @@ -94,20 +94,7 @@ Implementation of the 'validate' verb for HackMyResume. }; try { obj = safeLoadJSON(t); - if (obj.ex) { - if (obj.ex.operation === 'parse') { - errCode = HMSTATUS.parseError; - ret.status = 'broken'; - } else { - errCode = HMSTATUS.readError; - ret.status = 'missing'; - } - ret.error = { - fluenterror: errCode, - inner: obj.ex.inner, - quiet: errCode === HMSTATUS.readError - }; - } else { + if (!obj.ex) { if (obj.json.basics) { ret.schema = 'jars'; } else { @@ -123,11 +110,26 @@ Implementation of the 'validate' verb for HackMyResume. if (!ret.isValid) { ret.violations = validate.errors; } + } else { + if (obj.ex.operation === 'parse') { + errCode = HMSTATUS.parseError; + ret.status = 'broken'; + } else { + errCode = HMSTATUS.readError; + ret.status = 'missing'; + } + ret.error = { + fluenterror: errCode, + inner: obj.ex.inner, + quiet: errCode === HMSTATUS.readError + }; } } catch (_error) { - ret.error = _error; + ret.error = { + fluenterror: HMSTATUS.validateError, + inner: _error + }; } - ret.schema = ret.schema.replace('jars', 'JSON Resume'); this.stat(HMEVENT.afterValidate, ret); return ret; }; diff --git a/src/cli/error.coffee b/src/cli/error.coffee index 98b56be..d482799 100644 --- a/src/cli/error.coffee +++ b/src/cli/error.coffee @@ -222,6 +222,10 @@ assembleError = ( ex ) -> msg = printf M2C( this.msgs.createError.msg ), ex.inner.path etype = 'error' + when HMSTATUS.validateError + msg = printf M2C( @msgs.validateError.msg ), ex.inner.toString() + etype = 'error' + msg: msg # The error message to display withStack: withStack # Whether to include the stack quit: quit diff --git a/src/cli/msg.yml b/src/cli/msg.yml index 3161e3b..b60bdb2 100644 --- a/src/cli/msg.yml +++ b/src/cli/msg.yml @@ -44,7 +44,7 @@ events: - "INVALID" - "BROKEN" - "MISSING" - - "UNKNOWN" + - "ERROR" beforePeek: msg: - Peeking at **%s** in **%s** @@ -107,3 +107,5 @@ errors: msg: Failed to create **'%s'**. exiting: msg: Exiting with status code **%s**. + validateError: + msg: "An error occurred during validation:\n%s" diff --git a/src/cli/out.coffee b/src/cli/out.coffee index 1065188..3d997cc 100644 --- a/src/cli/out.coffee +++ b/src/cli/out.coffee @@ -148,7 +148,8 @@ module.exports = class OutputHandler when 'invalid' then style = 'yellow'; adj = msgs[2] when 'broken' then style = 'red'; adj = msgs[3] when 'missing' then style = 'red'; adj = msgs[4] - evt.schema = evt.schema.toUpperCase() + when 'unknown' then style = 'red'; adj = msgs[5] + evt.schema = evt.schema.replace('jars','JSON Resume').toUpperCase() L(M2C( msgs[0], 'white' ) + chalk[style].bold(adj), evt.file, evt.schema) if evt.violations diff --git a/src/core/status-codes.coffee b/src/core/status-codes.coffee index d8b9515..bf7cce3 100644 --- a/src/core/status-codes.coffee +++ b/src/core/status-codes.coffee @@ -32,3 +32,4 @@ module.exports = invalidParamCount: 23 missingParam: 24 createError: 25 + validateError: 26 diff --git a/src/verbs/validate.coffee b/src/verbs/validate.coffee index 4ef892e..14e9133 100644 --- a/src/verbs/validate.coffee +++ b/src/verbs/validate.coffee @@ -21,6 +21,8 @@ safeLoadJSON = require '../utils/safe-json-loader' ###* An invokable resume validation command. ### module.exports = class ValidateVerb extends Verb + + constructor: -> super 'validate', _validate @@ -37,8 +39,6 @@ _validate = (sources, unused, opts) -> fresh: require 'fresca' jars: require '../core/resume.json' - # Validate input resumes. Return a { file: , isValid: } object for - # each resume valid, invalid, or broken. results = _.map sources, (t) -> r = _validateOne.call @, t, validator, schemas, opts @err r.error.fluenterror, r.error if r.error @@ -69,31 +69,34 @@ _validateOne = (t, validator, schemas, opts) -> try - # Read and parse the resume JSON + # Read and parse the resume JSON. Won't throw. obj = safeLoadJSON t - if obj.ex + # If success, validate the resume against the schema + if !obj.ex + if obj.json.basics then ret.schema = 'jars' else ret.schema = 'fresh' + validate = validator schemas[ ret.schema ], # Note [1] + formats: { date: /^\d{4}(?:-(?:0[0-9]{1}|1[0-2]{1})(?:-[0-9]{2})?)?$/ } + ret.isValid = validate obj.json + ret.status = if ret.isValid then 'valid' else 'invalid' + ret.violations = validate.errors if !ret.isValid + + # If failure, package JSON read/parse errors + else if obj.ex.operation == 'parse' errCode = HMSTATUS.parseError ret.status = 'broken' else errCode = HMSTATUS.readError ret.status = 'missing' - ret.error = fluenterror: errCode, inner: obj.ex.inner, quiet: errCode == HMSTATUS.readError - - else - # Set up a FRESH or JSON Resume validator - if obj.json.basics then ret.schema = 'jars' else ret.schema = 'fresh' - validate = validator schemas[ ret.schema ], # Note [1] - formats: { date: /^\d{4}(?:-(?:0[0-9]{1}|1[0-2]{1})(?:-[0-9]{2})?)?$/ } - # Validate the resume against the schema - ret.isValid = validate obj.json - ret.status = if ret.isValid then 'valid' else 'invalid' - ret.violations = validate.errors if !ret.isValid + ret.error = + fluenterror: errCode, + inner: obj.ex.inner, + quiet: errCode == HMSTATUS.readError catch - ret.error = _error + # Package any unexpected exceptions + ret.error = fluenterror: HMSTATUS.validateError, inner: _error - ret.schema = ret.schema.replace 'jars', 'JSON Resume' @stat HMEVENT.afterValidate, ret ret