Throw error when non-Buffer specified instead of crashing
This commit is contained in:
parent
5adf9c47fc
commit
6b7ec621e3
2 changed files with 13 additions and 3 deletions
|
@ -53,13 +53,17 @@ v8::Local<v8::Value> Clipboard::ReadBuffer(const std::string& format_string,
|
||||||
args->isolate(), data.data(), data.length()).ToLocalChecked();
|
args->isolate(), data.data(), data.length()).ToLocalChecked();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Clipboard::WriteBuffer(const std::string& format_string,
|
void Clipboard::WriteBuffer(const std::string& format,
|
||||||
const v8::Local<v8::Value> buffer,
|
const v8::Local<v8::Value> buffer,
|
||||||
mate::Arguments* args) {
|
mate::Arguments* args) {
|
||||||
auto format = ui::Clipboard::GetFormatType(format_string);
|
if (!node::Buffer::HasInstance(buffer)) {
|
||||||
|
args->ThrowError("buffer must be a node Buffer");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
ui::ScopedClipboardWriter writer(GetClipboardType(args));
|
ui::ScopedClipboardWriter writer(GetClipboardType(args));
|
||||||
writer.WriteData(node::Buffer::Data(buffer), node::Buffer::Length(buffer),
|
writer.WriteData(node::Buffer::Data(buffer), node::Buffer::Length(buffer),
|
||||||
format);
|
ui::Clipboard::GetFormatType(format));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Clipboard::Write(const mate::Dictionary& data, mate::Arguments* args) {
|
void Clipboard::Write(const mate::Dictionary& data, mate::Arguments* args) {
|
||||||
|
|
|
@ -109,6 +109,12 @@ describe('clipboard module', function () {
|
||||||
clipboard.writeBuffer('public.utf8-plain-text', buffer)
|
clipboard.writeBuffer('public.utf8-plain-text', buffer)
|
||||||
assert.equal(clipboard.readText(), 'writeBuffer')
|
assert.equal(clipboard.readText(), 'writeBuffer')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('throws an error when a non-Buffer is specified', () => {
|
||||||
|
assert.throws(() => {
|
||||||
|
clipboard.writeBuffer('public.utf8-plain-text', 'hello')
|
||||||
|
}, /buffer must be a node Buffer/)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('clipboard.readBuffer(format)', function () {
|
describe('clipboard.readBuffer(format)', function () {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue