Handle with care
Fehlerbehandlung in JavaScript

Friday 8 November 13
WER BIN ICH?

•

Sebastian Springer

•

https://github.com/sspringer82

•

@basti_springer

Friday 8 November 13
Friday 8 November 13
Was ist ein Fehler?

☠
Friday 8 November 13
Was ist ein Fehler?

☠
Whoops, this should not happen.
Friday 8 November 13
Fehlertypen

Friday 8 November 13
Fehlertypen

Friday 8 November 13
Fehlertypen

Friday 8 November 13
Fehlertypen

Friday 8 November 13
Fehlertypen

Friday 8 November 13
Fehlertypen

Friday 8 November 13
Eigene Fehler

throw new Error('An error occurred');

Friday 8 November 13
Eigene Fehler
var MyErr = function (message) {
this.message = message;
this.name = 'MyErr';
}
MyErr.prototype = new Error(...
Friday 8 November 13
Abfangen von Fehlern
try {
throw new Error('Hello World');
} catch (e) {
console.log(e);
} finally {
console.log('Finished...
Abfangen von Fehlern
try {
} catch (RangeError e) {
}

Friday 8 November 13
Abfangen von Fehlern
try {
} catch (RangeError e) {
}

Friday 8 November 13
Abfangen von Fehlern
try {
} catch (e) {
if (e instanceof RangeError) {
}
}

Friday 8 November 13
Callbacks

Friday 8 November 13
Fehler in Callbacks
Applikation

Friday 8 November 13

Asynchrone
Ausführung
Fehler in Callbacks
Applikation

Friday 8 November 13

Asynchrone
Ausführung
Fehler in Callbacks
Asynchrone
Ausführung

Applikation
Operation

Friday 8 November 13
Fehler in Callbacks
Asynchrone
Ausführung

Applikation
Operation

Friday 8 November 13
Fehler in Callbacks
Asynchrone
Ausführung

Applikation
Operation

☠☂ ☏☣☢

Friday 8 November 13
Fehler in Callbacks
Asynchrone
Ausführung

Applikation
Operation

Callback

Friday 8 November 13

☠☂ ☏☣☢
Fehler in Callbacks
Asynchrone
Ausführung

Applikation
Operation

Callback

Friday 8 November 13

☠☂ ☏☣☢
Fehler in Callbacks
function myTimeout() {
throw new Error("whoops");
}

Friday 8 November 13
Fehler in Callbacks
function myTimeout() {
throw new Error("whoops");
}
try {
setTimeout("myTimeout()", 0);
} catch (e) {
...
Fehler in Callbacks

☠

function myTimeout() {
throw new Error("whoops");
}
try {
setTimeout("myTimeout()", 0);
} catch (e...
Friday 8 November 13
Fehler in Callbacks
$.ajax({
url: ‘/something’,
success: function (data) {
...
},
error: function (data) {
...
}
});

Frid...
Friday 8 November 13
Fehler in Callbacks
var fs = require('fs');
fs.readFile('file', function (err, d) {
...
});

Friday 8 November 13
Fehler in Callbacks
var fs = require('fs');
fs.readFile('file', function (err, d) {
...
});
null

Friday 8 November 13
Fehler in Callbacks
Error {
errno: ...
code: ...
path: ...
message: ...
}

Friday 8 November 13
Events

Friday 8 November 13
Error Events
Subject

Friday 8 November 13

Observer
Error Events
Subject

Friday 8 November 13

Observer
Error Events
Subject

Friday 8 November 13

Observer
Error Events
Subject

Observer
on(‘error’, function...

Friday 8 November 13
Error Events
Subject

Observer
on(‘error’, function...

☠☂ ☏☣☢

Friday 8 November 13
Error Events
Subject

Observer
on(‘error’, function...

☠☂ ☏☣☢

trigger(‘error’, {...

Friday 8 November 13
Error Events
Subject

Observer
on(‘error’, function...

☠☂ ☏☣☢

trigger(‘error’, {...

Friday 8 November 13
Promises
http://wiki.commonjs.org/wiki/Promises

Friday 8 November 13
Promises
In Javascript, promises are objects which represent the pending result of
an asynchronous operation.
(Martin Fowl...
Promises
$.ajax({
url: ‘/my/url’
}).done(function (data) {
...
}).fail(function (data) {
...
}).always(function (data) {
....
Promises
function promisedFunc() {
var def = $.Deferred();
// do something asynchronous
// success
def.resolve();
// fail
...
Promises
promisedFunc().done(function () {
// success
}).fail(function () {
// failure
}).always(function () {
// success ...
Promises
$.when(
promisedFunc(),
promisedFunc()
).then(success, failure);

Friday 8 November 13
Promises
•

http://api.jquery.com/category/deferred-object/
Client only

•

https://github.com/kriskowal/q
Client + Server...
Node.js Domain

Friday 8 November 13
Node domain
var domain = require(‘domain’).create();
domain.on(‘error’, function (err) {
console.log(err);
});
domain.run(...
Node Domain
Wird ausgeführt bei:

•
•

Friday 8 November 13

error-Events
Exceptions
window.onerror

Friday 8 November 13
window.onerror
catch it all!

Friday 8 November 13
window.onerror
window.onerror = function (msg, url, nr)
{
...
return true;
}

Friday 8 November 13
window.onerror
window.onerror

Code

Friday 8 November 13
und jetzt?

Friday 8 November 13
und jetzt?
Logging FTW!

Friday 8 November 13
Logging

Client

Friday 8 November 13

Server
Logging

Client

Server
☠☂ ☏☣☢

Friday 8 November 13
Logging

Log Message

Client
☠☂ ☏☣☢

Friday 8 November 13

Server
Logging

Log Message

Client
☠☂ ☏☣☢

Friday 8 November 13

Server
Logging
window.onerror = function (msg, url, nr)
{
$.ajax({
url: ‘/logger’
data: {
msg: msg,
url: url,
nr: nr
}
});
return...
Logging
Fire and Forget

Friday 8 November 13
Fragen?

Friday 8 November 13
Sebastian Springer
sebastian.springer@mayflower.de
Mayflower GmbH
Mannhardtstr. 6
80538 München
Deutschland
@basti_springe...
Upcoming SlideShare
Loading in...5
×

Error handling in JavaScript

1,193

Published on

My Talk at

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,193
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
28
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×