BVB Source Codes

waterline Show build-usage-error.js Source code

Return Download waterline: download build-usage-error.js Source code - Download waterline Source code - Type:.js
  1. /**
  2.  * Module dependencies
  3.  */
  4.  
  5. var util = require('util');
  6. var assert = require('assert');
  7. var _ = require('@sailshq/lodash');
  8. var flaverr = require('flaverr');
  9.  
  10.  
  11.  
  12. /**
  13.  * Module constants
  14.  */
  15.  
  16.  
  17. // Precompiled error message templates, one for each variety of recognized usage error.
  18. // (Precompiled by Lodash into callable functions that return strings.  Pass in `details` to use.)
  19. var USAGE_ERR_MSG_TEMPLATES = {
  20.  
  21.   E_NOOP: _.template(
  22.     'Query is a no-op.\n'+
  23.     '(It would have no effect and retrieve no useful information.)\n'+
  24.     '\n'+
  25.     'Details:\n'+
  26.     '  <%= details %>'+
  27.     '\n'
  28.     // ===============================================================================================
  29.     // NOTE: this error (^^^^^^) is so that there's some kind of default handling for
  30.     // the no-op case.  This generic error is not always relevant or good.  And anyway,
  31.     // most methods should handle E_NOOP explicitly.
  32.     //
  33.     // For example, if `.findOne()` notices an E_NOOP when forging, it simply swallows the error
  34.     // and calls its callback in exactly the same way as it would if specified criteria MIGHT have
  35.     // matched something but didn't.  The fact that it NEVER could have matched anything doesn't
  36.     // particularly matter from a userland perspective, and since certain parts of `criteria` and
  37.     // other query keys are often built _dynamically_, any more aggressive failure in this case would
  38.     // be inconsiderate, at best.  Thus it makes sense to avoid considering this an error whenever
  39.     // possible.
  40.     // ===============================================================================================
  41.   ),
  42.  
  43.   E_INVALID_META: _.template(
  44.     'Invalid value provided for `meta`.\n'+
  45.     'Details:\n'+
  46.     '  <%= details %>'+
  47.     '\n'
  48.   ),
  49.  
  50.   E_INVALID_CRITERIA: _.template(
  51.     'Invalid criteria.\n'+
  52.     'Refer to the docs for up-to-date info on query language syntax:\n'+
  53.     'http://sailsjs.com/docs/concepts/models-and-orm/query-language\n'+
  54.     '\n'+
  55.     'Details:\n'+
  56.     '  <%= details %>'+
  57.     '\n'
  58.   ),
  59.  
  60.   E_INVALID_POPULATES: _.template(
  61.     'Invalid populate(s).\n'+
  62.     'Details:\n'+
  63.     '  <%= details %>'+
  64.     '\n'
  65.   ),
  66.  
  67.   E_INVALID_NUMERIC_ATTR_NAME: _.template(
  68.     'Invalid numeric attr name.\n'+
  69.     'Details:\n'+
  70.     '  <%= details %>'+
  71.     '\n'
  72.   ),
  73.  
  74.   E_INVALID_STREAM_ITERATEE: _.template(
  75.     'Invalid iteratee function.\n'+
  76.     'Details:\n'+
  77.     '  <%= details %>'+
  78.     '\n'
  79.   ),
  80.  
  81.   E_INVALID_NEW_RECORD: _.template(
  82.     'Invalid initial data for new record.\n'+
  83.     'Details:\n'+
  84.     '  <%= details %>'+
  85.     '\n'
  86.   ),
  87.  
  88.   E_INVALID_NEW_RECORDS: _.template(
  89.     'Invalid initial data for new records.\n'+
  90.     'Details:\n'+
  91.     '  <%= details %>'+
  92.     '\n'
  93.   ),
  94.  
  95.   E_INVALID_VALUES_TO_SET: _.template(
  96.     'Invalid data-- cannot perform update with the provided values.\n'+
  97.     'Details:\n'+
  98.     '  <%= details %>'+
  99.     '\n'
  100.   ),
  101.  
  102.   E_INVALID_TARGET_RECORD_IDS: _.template(
  103.     'Invalid target record id(s).\n'+
  104.     'Details:\n'+
  105.     '  <%= details %>'+
  106.     '\n'
  107.   ),
  108.  
  109.   E_INVALID_COLLECTION_ATTR_NAME: _.template(
  110.     'Invalid collection attr name.\n'+
  111.     'Details:\n'+
  112.     '  <%= details %>'+
  113.     '\n'
  114.   ),
  115.  
  116.   E_INVALID_ASSOCIATED_IDS: _.template(
  117.     'Invalid associated id(s).\n'+
  118.     'Details:\n'+
  119.     '  <%= details %>'+
  120.     '\n'
  121.   ),
  122. };
  123.  
  124.  
  125.  
  126. /**
  127.  * buildUsageError()
  128.  *
  129.  * Build a new Error instance from the provided metadata.
  130.  *
  131.  * > Currently, this is designed for use with the `forgeStageTwoQuery()` utility, and its recognized
  132.  * > error codes are all related to that use case.  But the idea is that, over time, this can also
  133.  * > be used with any other sorts of new, end-developer-facing usage errors.
  134.  *
  135.  * ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  136.  * @param {String} code                 [e.g. 'E_INVALID_CRITERIA']
  137.  * @param {String} details              [e.g. 'The provided criteria contains an unrecognized property (`foo`):\n\'bar\'']
  138.  * @param {String} modelIdentity        [e.g. 'user']
  139.  * ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  140.  * @returns {Error}
  141.  *          @property {String} name   (==> 'UsageError')
  142.  *          @property {String} message  [composed from `details` and a built-in template]
  143.  *          @property {String} stack    [built automatically by `new Error()`]
  144.  *          @property {String} code     [the specified `code`]
  145.  *          @property {String} details  [the specified `details`]
  146.  *
  147.  * > The returned Error will have normalized properties and a standard,
  148.  * > nicely-formatted error message built from stitching together the
  149.  * > provided pieces of information.
  150.  * >
  151.  * > Note that, until we do automatic munging of stack traces, using
  152.  * > this utility adds another internal item to the top of the trace.
  153.  */
  154.  
  155. module.exports = function buildUsageError(code, details, modelIdentity) {
  156.  
  157.   assert(_.isString(code), '`code` must be provided as a string, but instead, got: '+util.inspect(code, {depth:5})+'');
  158.   assert(_.isString(details), '`details` must be provided as a string, but instead got: '+util.inspect(details, {depth:5})+'');
  159.   assert(_.isString(modelIdentity), '`modelIdentity` must be provided as a string, but instead, got: '+util.inspect(code, {depth:5})+'');
  160.  
  161.   // Look up standard template for this particular error code.
  162.   if (!USAGE_ERR_MSG_TEMPLATES[code]) {
  163.     throw new Error('Consistency violation: Unrecognized error code: '+code);
  164.   }
  165.  
  166.   // Build error message.
  167.   var errorMessage = USAGE_ERR_MSG_TEMPLATES[code]({
  168.     details: details,
  169.     modelIdentity: modelIdentity
  170.   });
  171.  
  172.   // Instantiate Error.
  173.   // (This builds the stack trace.)
  174.   var err = new Error(errorMessage);
  175.  
  176.   // Flavor the error with the appropriate `code`, direct access to the provided `details`,
  177.   // and a consistent "name" (i.e. so it reads nicely when logged.)
  178.   err = flaverr({
  179.     name: 'UsageError',
  180.     code: code,
  181.     details: details,
  182.     modelIdentity: modelIdentity
  183.   }, err);
  184.  
  185.   // That's it!
  186.   // Send it on back.
  187.   return err;
  188.  
  189. };
  190.  
downloadbuild-usage-error.js Source code - Download waterline Source code
Related Source Codes/Software:
notepad-plus-plus - Notepad++ official repository h... 2017-01-10
che - Eclipse Che: Next-generation Eclipse IDE. Open sou... 2017-01-10
Gource - oftware version control visualization ... 2017-01-10
FDFullscreenPopGesture - A UINavigationController's category to enable full... 2017-01-10
node-style-guide - A guide for styling your node.js / JavaScript code... 2017-01-09
Workerman - An asynchronous event driven PHP framework for eas... 2017-01-10
structor - An advanced visual editor for React components ... 2017-01-10
golearn - Machine Learning for Go 2017-01-10
poisontap - Exploits locked/password protected computers over ... 2017-01-10
kcptun - A Simple UDP Tunnel Based On KCP 2017-01-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top