Error handling in JavaScript

1,645 views

Published on

My Talk at

Published in: Technology
  • Be the first to comment

Error handling in JavaScript

  1. 1. Handle with care Fehlerbehandlung in JavaScript Friday 8 November 13
  2. 2. WER BIN ICH? • Sebastian Springer • https://github.com/sspringer82 • @basti_springer Friday 8 November 13
  3. 3. Friday 8 November 13
  4. 4. Was ist ein Fehler? ☠ Friday 8 November 13
  5. 5. Was ist ein Fehler? ☠ Whoops, this should not happen. Friday 8 November 13
  6. 6. Fehlertypen Friday 8 November 13
  7. 7. Fehlertypen Friday 8 November 13
  8. 8. Fehlertypen Friday 8 November 13
  9. 9. Fehlertypen Friday 8 November 13
  10. 10. Fehlertypen Friday 8 November 13
  11. 11. Fehlertypen Friday 8 November 13
  12. 12. Eigene Fehler throw new Error('An error occurred'); Friday 8 November 13
  13. 13. Eigene Fehler var MyErr = function (message) { this.message = message; this.name = 'MyErr'; } MyErr.prototype = new Error(); MyErr.prototype.constructor = MyErr; Friday 8 November 13
  14. 14. Friday 8 November 13
  15. 15. Abfangen von Fehlern try { throw new Error('Hello World'); } catch (e) { console.log(e); } finally { console.log('Finished'); } Friday 8 November 13
  16. 16. Abfangen von Fehlern try { } catch (RangeError e) { } Friday 8 November 13
  17. 17. Abfangen von Fehlern try { } catch (RangeError e) { } Friday 8 November 13
  18. 18. Abfangen von Fehlern try { } catch (e) { if (e instanceof RangeError) { } } Friday 8 November 13
  19. 19. Callbacks Friday 8 November 13
  20. 20. Fehler in Callbacks Applikation Friday 8 November 13 Asynchrone Ausführung
  21. 21. Fehler in Callbacks Applikation Friday 8 November 13 Asynchrone Ausführung
  22. 22. Fehler in Callbacks Asynchrone Ausführung Applikation Operation Friday 8 November 13
  23. 23. Fehler in Callbacks Asynchrone Ausführung Applikation Operation Friday 8 November 13
  24. 24. Fehler in Callbacks Asynchrone Ausführung Applikation Operation ☠☂ ☏☣☢ Friday 8 November 13
  25. 25. Fehler in Callbacks Asynchrone Ausführung Applikation Operation Callback Friday 8 November 13 ☠☂ ☏☣☢
  26. 26. Fehler in Callbacks Asynchrone Ausführung Applikation Operation Callback Friday 8 November 13 ☠☂ ☏☣☢
  27. 27. Fehler in Callbacks function myTimeout() { throw new Error("whoops"); } Friday 8 November 13
  28. 28. Fehler in Callbacks function myTimeout() { throw new Error("whoops"); } try { setTimeout("myTimeout()", 0); } catch (e) { console.log(e); } Friday 8 November 13
  29. 29. Fehler in Callbacks ☠ function myTimeout() { throw new Error("whoops"); } try { setTimeout("myTimeout()", 0); } catch (e) { console.log(e); } Friday 8 November 13
  30. 30. Friday 8 November 13
  31. 31. Fehler in Callbacks $.ajax({ url: ‘/something’, success: function (data) { ... }, error: function (data) { ... } }); Friday 8 November 13
  32. 32. Friday 8 November 13
  33. 33. Fehler in Callbacks var fs = require('fs'); fs.readFile('file', function (err, d) { ... }); Friday 8 November 13
  34. 34. Fehler in Callbacks var fs = require('fs'); fs.readFile('file', function (err, d) { ... }); null Friday 8 November 13
  35. 35. Fehler in Callbacks Error { errno: ... code: ... path: ... message: ... } Friday 8 November 13
  36. 36. Events Friday 8 November 13
  37. 37. Error Events Subject Friday 8 November 13 Observer
  38. 38. Error Events Subject Friday 8 November 13 Observer
  39. 39. Error Events Subject Friday 8 November 13 Observer
  40. 40. Error Events Subject Observer on(‘error’, function... Friday 8 November 13
  41. 41. Error Events Subject Observer on(‘error’, function... ☠☂ ☏☣☢ Friday 8 November 13
  42. 42. Error Events Subject Observer on(‘error’, function... ☠☂ ☏☣☢ trigger(‘error’, {... Friday 8 November 13
  43. 43. Error Events Subject Observer on(‘error’, function... ☠☂ ☏☣☢ trigger(‘error’, {... Friday 8 November 13
  44. 44. Promises http://wiki.commonjs.org/wiki/Promises Friday 8 November 13
  45. 45. Promises In Javascript, promises are objects which represent the pending result of an asynchronous operation. (Martin Fowler) Friday 8 November 13
  46. 46. Promises $.ajax({ url: ‘/my/url’ }).done(function (data) { ... }).fail(function (data) { ... }).always(function (data) { ... }); Friday 8 November 13
  47. 47. Promises function promisedFunc() { var def = $.Deferred(); // do something asynchronous // success def.resolve(); // fail def.reject(); return def.promise(); } Friday 8 November 13
  48. 48. Promises promisedFunc().done(function () { // success }).fail(function () { // failure }).always(function () { // success & failure }); Friday 8 November 13
  49. 49. Promises $.when( promisedFunc(), promisedFunc() ).then(success, failure); Friday 8 November 13
  50. 50. Promises • http://api.jquery.com/category/deferred-object/ Client only • https://github.com/kriskowal/q Client + Server • https://github.com/kriszyp/node-promise Server only Friday 8 November 13
  51. 51. Node.js Domain Friday 8 November 13
  52. 52. Node domain var domain = require(‘domain’).create(); domain.on(‘error’, function (err) { console.log(err); }); domain.run(function () { ... }); Friday 8 November 13
  53. 53. Node Domain Wird ausgeführt bei: • • Friday 8 November 13 error-Events Exceptions
  54. 54. window.onerror Friday 8 November 13
  55. 55. window.onerror catch it all! Friday 8 November 13
  56. 56. window.onerror window.onerror = function (msg, url, nr) { ... return true; } Friday 8 November 13
  57. 57. window.onerror window.onerror Code Friday 8 November 13
  58. 58. und jetzt? Friday 8 November 13
  59. 59. und jetzt? Logging FTW! Friday 8 November 13
  60. 60. Logging Client Friday 8 November 13 Server
  61. 61. Logging Client Server ☠☂ ☏☣☢ Friday 8 November 13
  62. 62. Logging Log Message Client ☠☂ ☏☣☢ Friday 8 November 13 Server
  63. 63. Logging Log Message Client ☠☂ ☏☣☢ Friday 8 November 13 Server
  64. 64. Logging window.onerror = function (msg, url, nr) { $.ajax({ url: ‘/logger’ data: { msg: msg, url: url, nr: nr } }); return true; } Friday 8 November 13
  65. 65. Logging Fire and Forget Friday 8 November 13
  66. 66. Fragen? Friday 8 November 13
  67. 67. Sebastian Springer sebastian.springer@mayflower.de Mayflower GmbH Mannhardtstr. 6 80538 München Deutschland @basti_springer https://github.com/sspringer82 Friday 8 November 13

×