๋ฆด๋ฆฌ์ค 3.23.2์์ es6-error
(https://www.npmjs.com/package/es6-error)๋ก ์์ฑ๋ ์ค๋ฅ๋ฅผ ์ผ์ผํค๋ ์๋ก์ด ๊ธฐ๋ฅ "Sensible non-Error exception serializer"(#1253) ์ค๋ฅ ๋ด์ฉ์ ๋ณ๊ฒฝํฉ๋๋ค.
์ด ์ค์ captureException
ํจ์์์ ๋ณ๊ฒฝ๋๊ณ ์์ต๋๋ค.
options = this._getCaptureExceptionOptionsFromPlainObject(options, ex);
๋ ๊ตฌ์ฒด์ ์ผ๋ก ๋งํ๋ฉด ๋ค์์ ๋ด ์ฝ๋์ ๋๋ค.
import ExtendableError from 'es6-error'
class CustomError extends ExtendableError {
constructor(code, message) {
super(message)
this.name ='CustomError'
this.code = code
}
}
export default CustomError
๊ทธ๋ฐ ๋ค์ ์ฝ๋์์:
Raven.captureException(new CustomError('code123', 'not working'));
// this will cause the error message to be "Non-Error exception captured with keys: code"
๊ทธ๊ฒ์ JS ๋ชจ๋์ ๋งค์ฐ ์ด์ํ ๋์์ธ ๊ฒ ๊ฐ์ต๋๋ค.
๋ค์์ ์ฌ์ฉํ ๋:
import ExtendableError from 'es6-error'
class CustomError extends ExtendableError {
constructor(code, message) {
super(message)
this.name ='CustomError'
this.code = code
}
}
function isPlainObject(what) {
return Object.prototype.toString.call(what) === '[object Object]';
}
function isError(value) {
switch ({}.toString.call(value)) {
case '[object Error]':
return true;
case '[object Exception]':
return true;
case '[object DOMException]':
return true;
default:
return value instanceof Error;
}
}
function isErrorEvent(value) {
return Object.prototype.toString.call(value) === '[object ErrorEvent]';
}
var ex = new CustomError('code123', 'not working');
console.log('isPlainObject:', isPlainObject(ex))
console.log('isError:', isError(ex))
console.log('isErrorEvent:', isErrorEvent(ex))
true/true/false
์๋ชป ๋ณด๊ณ ํฉ๋๋ค.
๊ทธ๋ฌ๋ import
๋ฌธ์ ์ฝ๋ ์์ฒด๋ก ๋์ฒดํ ๋
class ExtendableError extends Error {
constructor(message = '') {
super(message);
// extending Error is weird and does not propagate `message`
Object.defineProperty(this, 'message', {
configurable: true,
enumerable : false,
value : message,
writable : true,
});
Object.defineProperty(this, 'name', {
configurable: true,
enumerable : false,
value : this.constructor.name,
writable : true,
});
if (Error.hasOwnProperty('captureStackTrace')) {
Error.captureStackTrace(this, this.constructor);
return;
}
Object.defineProperty(this, 'stack', {
configurable: true,
enumerable : false,
value : (new Error(message)).stack,
writable : true,
});
}
}
false/true/false
๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ๋ณด๊ณ ํฉ๋๋ค.
์ ๊ทธ๋ฐ์ง ๋๋ฒ๊น
์ ํด๋ณด๊ณ ์ ์ ์์ผ๋ฉด ๋จผ์ isError
๋ฅผ ํต๊ณผํ๋๋ก ์ํ๋ฅผ ๋ฐ๊พธ๊ฒ ์ต๋๋ค.
@zivl ๋ก์ปฌ์์ ํ
์คํธํ๊ณ ํด๋น ์ฝ๋๋ก ์์ฒด custom-error.js
ํ์ผ์ ๋ง๋ค๊ณ ๋ชจ๋๋ก ๊ฐ์ ธ์ค๋ฉด ์ฌ๋ฐ๋ฅด๊ฒ ์๋ํฉ๋๋ค.
๋ฌธ์ ๋ ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ฝ๋๋ฅผ "์๋ ๊ทธ๋๋ก" ๋ด๋ณด๋ด์ง ์๊ณ Babel์ ์ฌ์ฉํ์ฌ ๋ฏธ๋ฆฌ ์ปดํ์ผํ๋ค๋ ๊ฒ์ ๋๋ค.
Sooo ๊ทธ๊ฒ์ "์ผ์ข
์"์ค๋ฅ๋ก ๋๋์ง๋ง ์ค์ ๋ก๋ ๊ทธ๋ ์ง ์์ต๋๋ค. ํน์ ์์ฑ์ ์์ํ๊ณ prototype
๊ฐ Error
์์ฒด๋ก ์ค์ ๋ ์ผ๋ฐ ๊ฐ์ฒด์
๋๋ค.
๋ถํํ๋ "์๋ขฐ๋ฉ๊ฑฐ ์ค๋ฅ"์ ๊ฐ๊ธฐ ๋๋ฌธ์ ์ํ ์์๋ฅผ ๋ณ๊ฒฝํ๋ ๊ฒ ์ธ์๋ ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ๋ฐฉ๋ฒ์ด ์์ต๋๋ค. ๊ทธ๊ฒ์ ์ค๋ฅ์ด๋ฉฐ ๋์์ ์ค๋ฅ๊ฐ ์๋๋๋ค.
@zivl ์ด 3.23.3
์ ์ถ์๋์์ต๋๋ค. ์ ๊ณ ๊ฐ์ฌํฉ๋๋ค!
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
@zivl ๋ก์ปฌ์์ ํ ์คํธํ๊ณ ํด๋น ์ฝ๋๋ก ์์ฒด
custom-error.js
ํ์ผ์ ๋ง๋ค๊ณ ๋ชจ๋๋ก ๊ฐ์ ธ์ค๋ฉด ์ฌ๋ฐ๋ฅด๊ฒ ์๋ํฉ๋๋ค.๋ฌธ์ ๋ ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ฝ๋๋ฅผ "์๋ ๊ทธ๋๋ก" ๋ด๋ณด๋ด์ง ์๊ณ Babel์ ์ฌ์ฉํ์ฌ ๋ฏธ๋ฆฌ ์ปดํ์ผํ๋ค๋ ๊ฒ์ ๋๋ค.
Sooo ๊ทธ๊ฒ์ "์ผ์ข ์"์ค๋ฅ๋ก ๋๋์ง๋ง ์ค์ ๋ก๋ ๊ทธ๋ ์ง ์์ต๋๋ค. ํน์ ์์ฑ์ ์์ํ๊ณ
prototype
๊ฐError
์์ฒด๋ก ์ค์ ๋ ์ผ๋ฐ ๊ฐ์ฒด์ ๋๋ค.๋ถํํ๋ "์๋ขฐ๋ฉ๊ฑฐ ์ค๋ฅ"์ ๊ฐ๊ธฐ ๋๋ฌธ์ ์ํ ์์๋ฅผ ๋ณ๊ฒฝํ๋ ๊ฒ ์ธ์๋ ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ๋ฐฉ๋ฒ์ด ์์ต๋๋ค. ๊ทธ๊ฒ์ ์ค๋ฅ์ด๋ฉฐ ๋์์ ์ค๋ฅ๊ฐ ์๋๋๋ค.