Открыть боковую панель
keewek
vscode-compare-helper
Коммиты
672ebdc9
Коммит
672ebdc9
создал
Ноя 09, 2020
по автору
keewek
Просмотр файлов
feat(exception): add exception module
test(exception): add tests
владелец
b52e8e9c
Изменения
2
Скрыть пробелы
Построчно
Рядом
src/exception.ts
0 → 100644
Просмотр файла @
672ebdc9
export
class
ErrorWithData
extends
Error
{
private
_data
:
any
;
private
_hasData
:
boolean
=
false
;
readonly
scope
:
string
;
constructor
(
message
:
string
|
undefined
,
options
?:
{
scope
?:
string
;
data
?:
any
;
})
{
super
(
message
);
this
.
scope
=
options
?.
scope
??
''
;
if
(
options
&&
'
data
'
in
options
)
{
this
.
data
=
options
.
data
;
}
}
set
data
(
data
:
any
)
{
this
.
_hasData
=
true
;
this
.
_data
=
data
;
}
get
data
():
any
{
return
this
.
_data
;
}
get
hasData
():
boolean
{
return
this
.
_hasData
;
}
scopedMessage
(
separator
:
string
=
'
:
'
):
string
{
if
(
this
.
scope
.
length
>
0
)
{
return
this
.
scope
+
separator
+
this
.
message
;
}
return
this
.
message
;
}
}
Object
.
defineProperty
(
ErrorWithData
.
prototype
,
'
name
'
,
{
value
:
'
ErrorWithData
'
});
src/test/suite/exception.test.ts
0 → 100644
Просмотр файла @
672ebdc9
import
*
as
assert
from
'
assert
'
;
import
{
ErrorWithData
}
from
'
../../exception
'
;
suite
(
'
Exception Test Suite
'
,
function
()
{
suite
(
'
ErrorWithData
'
,
function
()
{
test
(
'
name is "ErrorWithData"
'
,
function
()
{
const
e
=
new
ErrorWithData
(
'
msg
'
);
assert
.
strictEqual
(
e
.
name
,
'
ErrorWithData
'
);
});
test
(
'
scopedMessage without scope
'
,
function
()
{
const
e
=
new
ErrorWithData
(
'
msg
'
);
assert
.
strictEqual
(
e
.
scopedMessage
(),
'
msg
'
);
});
test
(
'
scopedMessage with scope
'
,
function
()
{
const
e
=
new
ErrorWithData
(
'
msg
'
,
{
scope
:
'
scope
'
});
assert
.
strictEqual
(
e
.
scopedMessage
(),
'
scope: msg
'
);
});
test
(
'
scopedMessage with scope and custom separator
'
,
function
()
{
const
e
=
new
ErrorWithData
(
'
msg
'
,
{
scope
:
'
scope
'
});
assert
.
strictEqual
(
e
.
scopedMessage
(
'
=
'
),
'
scope = msg
'
);
});
test
(
'
hasData is false with no data
'
,
function
()
{
const
e
=
new
ErrorWithData
(
'
msg
'
,
{
scope
:
'
scope
'
});
assert
.
strictEqual
(
e
.
hasData
,
false
);
});
test
(
'
data can be set to undefined
'
,
function
()
{
const
e
=
new
ErrorWithData
(
'
msg
'
,
{
scope
:
'
scope
'
,
data
:
undefined
});
assert
.
strictEqual
(
e
.
hasData
,
true
);
assert
.
strictEqual
(
e
.
data
,
undefined
);
});
});
});
Редактирование
Предварительный просмотр
Поддерживает Markdown
0%
Попробовать снова
или
прикрепить новый файл
.
Отмена
You are about to add
0
people
to the discussion. Proceed with caution.
Сначала завершите редактирование этого сообщения!
Отмена
Пожалуйста,
зарегистрируйтесь
или
войдите
чтобы прокомментировать