SlideShare una empresa de Scribd logo
1 de 67
Descargar para leer sin conexión
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();
MyErr.prototype.constructor = MyErr;

Friday 8 November 13
Friday 8 November 13
Abfangen von Fehlern
try {
throw new Error('Hello World');
} catch (e) {
console.log(e);
} finally {
console.log('Finished');
}

Friday 8 November 13
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) {
console.log(e);
}
Friday 8 November 13
Fehler in Callbacks

☠

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

Friday 8 November 13
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 Fowler)

Friday 8 November 13
Promises
$.ajax({
url: ‘/my/url’
}).done(function (data) {
...
}).fail(function (data) {
...
}).always(function (data) {
...
});

Friday 8 November 13
Promises
function promisedFunc() {
var def = $.Deferred();
// do something asynchronous
// success
def.resolve();
// fail
def.reject();
return def.promise();
}

Friday 8 November 13
Promises
promisedFunc().done(function () {
// success
}).fail(function () {
// failure
}).always(function () {
// success & failure
});

Friday 8 November 13
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

•

https://github.com/kriszyp/node-promise
Server only

Friday 8 November 13
Node.js Domain

Friday 8 November 13
Node domain
var domain = require(‘domain’).create();
domain.on(‘error’, function (err) {
console.log(err);
});
domain.run(function () {
...
});

Friday 8 November 13
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 true;
}
Friday 8 November 13
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_springer
https://github.com/sspringer82

Friday 8 November 13

Más contenido relacionado

Más de Sebastian Springer

Más de Sebastian Springer (20)

Schnelleinstieg in Angular
Schnelleinstieg in AngularSchnelleinstieg in Angular
Schnelleinstieg in Angular
 
Creating Enterprise Web Applications with Node.js
Creating Enterprise Web Applications with Node.jsCreating Enterprise Web Applications with Node.js
Creating Enterprise Web Applications with Node.js
 
Divide and Conquer – Microservices with Node.js
Divide and Conquer – Microservices with Node.jsDivide and Conquer – Microservices with Node.js
Divide and Conquer – Microservices with Node.js
 
From Zero to Hero – Web Performance
From Zero to Hero – Web PerformanceFrom Zero to Hero – Web Performance
From Zero to Hero – Web Performance
 
Von 0 auf 100 - Performance im Web
Von 0 auf 100 - Performance im WebVon 0 auf 100 - Performance im Web
Von 0 auf 100 - Performance im Web
 
A/B Testing mit Node.js
A/B Testing mit Node.jsA/B Testing mit Node.js
A/B Testing mit Node.js
 
Angular2
Angular2Angular2
Angular2
 
Einführung in React
Einführung in ReactEinführung in React
Einführung in React
 
JavaScript Performance
JavaScript PerformanceJavaScript Performance
JavaScript Performance
 
ECMAScript 6 im Produktivbetrieb
ECMAScript 6 im ProduktivbetriebECMAScript 6 im Produktivbetrieb
ECMAScript 6 im Produktivbetrieb
 
Streams in Node.js
Streams in Node.jsStreams in Node.js
Streams in Node.js
 
JavaScript Performance
JavaScript PerformanceJavaScript Performance
JavaScript Performance
 
Große Applikationen mit AngularJS
Große Applikationen mit AngularJSGroße Applikationen mit AngularJS
Große Applikationen mit AngularJS
 
Testing tools
Testing toolsTesting tools
Testing tools
 
Node.js Security
Node.js SecurityNode.js Security
Node.js Security
 
Typescript
TypescriptTypescript
Typescript
 
Reactive Programming
Reactive ProgrammingReactive Programming
Reactive Programming
 
Best Practices für TDD in JavaScript
Best Practices für TDD in JavaScriptBest Practices für TDD in JavaScript
Best Practices für TDD in JavaScript
 
Warum ECMAScript 6 die Welt ein Stückchen besser macht
Warum ECMAScript 6 die Welt ein Stückchen besser machtWarum ECMAScript 6 die Welt ein Stückchen besser macht
Warum ECMAScript 6 die Welt ein Stückchen besser macht
 
Lean Startup mit JavaScript
Lean Startup mit JavaScriptLean Startup mit JavaScript
Lean Startup mit JavaScript
 

Último

Último (20)

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 

Error handling in JavaScript