Guest User

Untitled

a guest
Apr 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. for (i = 0; i < len; i++) {
  2. try { this.example(arg[i]) }
  3. catch (e) { log(e) }
  4. }
  5.  
  6. function ErrorCollection(msg) {
  7. this.name = 'ErrorCollection';
  8. this.message = msg || '';
  9. this.errors = [];
  10. }
  11. ErrorCollection.prototype = Object.create(Error.prototype);
  12. ErrorCollection.prototype.constructor = ErrorCollection;
  13. ErrorCollection.prototype.push = function (e) {
  14. return this.errors.push(e);
  15. }
  16.  
  17. // ...
  18.  
  19. var i, ec = new ErrorCollection();
  20. for (i = 0; i < 10; ++i) {
  21. try {
  22. throw new Error('Error ' + i);
  23. } catch (e) {
  24. ec.push(e);
  25. }
  26. }
  27. // do something with ec, e.g.
  28. if (ec.errors.length) {
  29. console.log(ec, ec.errors);
  30. throw ec;
  31. }
  32.  
  33. var i, a = [];
  34. for (i = 0; i < arguments.length; ++i) {
  35. try { // assume everything will work
  36. this.example(arguments[i]);
  37. } catch (e) { // our assumption was wrong
  38. a.push({arg: i, val: arguments[i]});
  39. for (++i; i < arguments.length; ++i) { // loop from where we left off
  40. if (!this.valid_arg(arguments[i])) { // some costly test
  41. a.push({arg: i, val: arguments[i]});
  42. }
  43. }
  44. throw a; // throw the list of bad args up a level
  45. }
  46. }
Add Comment
Please, Sign In to add comment