help-find.util.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /**
  2. * Module dependencies
  3. */
  4. var _ = require('@sailshq/lodash');
  5. var flaverr = require('flaverr');
  6. // This is used by find.fixture.js.
  7. // It was originally and part of a benchmarking experiment, and its extrapolation
  8. // was found to have a positive impact on performance.
  9. module.exports = function helpFind(unused, metadata, finalCb) {
  10. if (unused) {
  11. finalCb(new Error('Consistency violation: Unexpected internal error occurred before beginning with any business logic. Details: '+unused.stack));
  12. return;
  13. }//-•
  14. // Now actually do stuff.
  15. // In this case, we'll just pretend, since this part doesn't matter.
  16. // (we just wait a few miliseconds, and then send back an array consisting
  17. // of one item: the `criteria` that was received.)
  18. setTimeout(function (){
  19. var fakeResult = [ metadata.criteria ];
  20. // Note that, as a way for our test cases to instrument the outcome,
  21. // we check `metadata.criteria` here, and if it happens to be `false`
  22. // or `null`, then we trigger an error instead.
  23. if (metadata.criteria === false) {
  24. return finalCb(flaverr('E_SOME_ERROR', new Error('Simulated failure (E_SOME_ERROR)')));
  25. }
  26. if (_.isNull(metadata.criteria)) {
  27. return finalCb(new Error('Simulated failure (catchall / misc. error)'));
  28. }
  29. return finalCb(undefined, fakeResult);
  30. }, 25);
  31. };