node.js error & debug leveling

Post on 10-May-2015

2.967 Views

Category:

Documents

11 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Node.jsError and Debug

Leveling

2013-05-25Yosuke Kumakura / @kumatch

Yosuke Kumakura (kumatch)

@kumatch

Feedtailor inc.

Current game: Path of Exile

Error and DebugLeveling

Leveling (game word)

Node で出現するエラーと

戦ってレベル上げを

しましょう。

Level 1

$ node test.js-bash: node: command not found

Level 2

$ pwd/path/to

$ node test.js

module.js:340 throw err; ^Error: Cannot find module '/path/to/test.js' at Function.Module._resolveFilename (module.js:338:15) at Function.Module._load (module.js:280:25) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:901:3

Level 3

/path/to/test.js:3### コメント開始記号が間違い // が正解 ^SyntaxError: Unexpected token ILLEGAL at Module._compile (module.js:439:25) ....

var level = 3;

### コメント開始記号が間違い // が正解

Level 4

/path/to/test.js:3var 54 = 42; ^^

SyntaxError: Unexpected number at Module._compile (module.js:439:25) ....

var level = 4;

var 54 = 42;

Level 5

/path/to/test.js:3console.log( foo ) ^

ReferenceError: foo is not defined at Object.<anonymous> (/path/to/test.js:1:75) ....

var level = 5;

console.log( foo )

Level 6

/path/to/test.js:3level();^

TypeError: number is not a function at Object.<anonymous> (/path/to/test.js:3:1) ....

var level = 6;

level();

Level 7

/path/to/test.js:4level.foo(); ^

TypeError: Object 7 has no method 'foo' at Object.<anonymous> (/path/to/test.js:4:7) ....

var level = 7;

level.toString();level.foo();

catch the error

同期処理と

非同期処理

非同期の形式は常に最後の引数として完了コールバックを受け取ります。引数として渡される完了コールバックはメソッドに依存しますが、最初の引数は常に例外のために予約されています。操作が成功で完了すると最初の引数は null または undefined となります。

同期の形式では、全ての例外はすぐにスローされます。例外は try/catch で捕まえることも、そのまま通過させることもできます。

File System (fs) モジュールDocument より抜粋

Level 8

fs.js:684 return binding.stat(pathModule._makeLong(path)); ^Error: ENOENT, no such file or directory 'nothing.txt' at Object.fs.statSync (fs.js:684:18) at Object.<anonymous> (/path/to/test.js:3:15) ....

var fs = require('fs');

var stat = fs.statSync('nothing.txt');

Level 9

{ [Error: ENOENT, no such file or directory 'nothing.txt'] errno: 34, code: 'ENOENT', path: 'invalid.txt', syscall: 'stat' }

var fs = require('fs');

try { var stat = fs.statSync('nothing.txt');} catch (err) { console.log(err);}

Level 10

Error: ENOENT, no such file or directory 'nothing.txt' at Object.fs.statSync (fs.js:684:18) at Object.<anonymous> (/path/to/test.js:4:19) ....

var fs = require('fs');

try { var stat = fs.statSync('nothing.txt');} catch (err) { console.log(err.stack);}

Level 11

{ [Error: ENOENT, stat 'nothing.txt'] errno: 34, code: 'ENOENT', path: 'nothing.txt' }undefined

var fs = require('fs');

fs.stat('nothing.txt', function (err, stats) { console.log(err);});

Level 12

var fs = require('fs');

try { fs.stat('nothing.txt', function (err, stats) { // do nothing... });} catch (err) { console.log(err);}

非同期の形式は常に最後の引数として完了コールバックを受け取ります。引数として渡される完了コールバックはメソッドに依存しますが、最初の引数は常に例外のために予約されています。操作が成功で完了すると最初の引数は null または undefined となります。

同期の形式では、全ての例外はすぐにスローされます。例外は try/catch で捕まえることも、そのまま通過させることもできます。

File System (fs) モジュールDocument より再度抜粋

socket.connect(port, [host], [connectListener])socket.connect(path, [connectListener])

与えられたソケットでコネクションをオープンします。 (中略...)

この関数は非同期です。ソケットが確立されると 'connect' イベントが生成されます。接続で問題があった場合は 'connect' イベントは生成されず、 例外とともに 'error' イベントが生成されます。

Net モジュール Document より抜粋

Level 13

events.js:72 throw er; // Unhandled 'error' event ^Error: connect ECONNREFUSED at errnoException (net.js:884:11) at Object.afterConnect [as oncomplete] (net.js:875:19)

var net = require('net');

var client = net.connect({ port: 8128 });

Level 14

{ [Error: connect ECONNREFUSED] code: 'ECONNREFUSED', errno: 'ECONNREFUSED', syscall: 'connect' }

var net = require('net');

var client = net.connect({ port: 8128 });

client.on('error', function (err) { console.log(err);});

Level 15var net = require('net');

try { var client = net.connect({ port: 8128 }); client.on('error', function (err) { // do nothing... });} catch (err) { console.log(err);}

Debugger

Level 16

$ node debug test.js

Level 17

$ node-inspector --web-port=8888 &$ node --debug-brk test.js

$ npm install -g node-inspector

Leveling まとめ

• エラーを読む

• メッセージ および Stack

• 機能毎のエラーハンドリングを行う

• Debugger を使う

Next levelings...

• Raise Errors

•Domains

top related