chore: improve nativeImage path converter error (#21243)

This commit is contained in:
Shelley Vohr 2019-11-22 19:16:43 -08:00 committed by GitHub
parent 34452ee69e
commit d20273f95b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 2 deletions

View file

@ -12,6 +12,7 @@
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/strings/pattern.h" #include "base/strings/pattern.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_restrictions.h" #include "base/threading/thread_restrictions.h"
#include "net/base/data_url.h" #include "net/base/data_url.h"
#include "shell/common/asar/asar_util.h" #include "shell/common/asar/asar_util.h"
@ -542,8 +543,18 @@ bool Converter<electron::api::NativeImage*>::FromV8(
base::FilePath path; base::FilePath path;
if (ConvertFromV8(isolate, val, &path)) { if (ConvertFromV8(isolate, val, &path)) {
*out = electron::api::NativeImage::CreateFromPath(isolate, path).get(); *out = electron::api::NativeImage::CreateFromPath(isolate, path).get();
// Should throw when failed to initialize from path. if ((*out)->image().IsEmpty()) {
return !(*out)->image().IsEmpty(); #if defined(OS_WIN)
const auto img_path = base::UTF16ToUTF8(path.value());
#else
const auto img_path = path.value();
#endif
isolate->ThrowException(v8::Exception::Error(
StringToV8(isolate, "Image could not be created from " + img_path)));
return false;
}
return true;
} }
*out = static_cast<electron::api::NativeImage*>( *out = static_cast<electron::api::NativeImage*>(

View file

@ -1,6 +1,7 @@
import { expect } from 'chai' import { expect } from 'chai'
import { Menu, Tray, nativeImage } from 'electron' import { Menu, Tray, nativeImage } from 'electron'
import { ifdescribe, ifit } from './spec-helpers' import { ifdescribe, ifit } from './spec-helpers'
import * as path from 'path'
describe('tray module', () => { describe('tray module', () => {
let tray: Tray let tray: Tray
@ -12,6 +13,15 @@ describe('tray module', () => {
tray = null as any tray = null as any
}) })
describe('new Tray', () => {
it('throws a descriptive error for a missing file', () => {
const badPath = path.resolve('I', 'Do', 'Not', 'Exist')
expect(() => {
tray = new Tray(badPath)
}).to.throw(/Image could not be created from .*/)
})
})
ifdescribe(process.platform === 'darwin')('tray get/set ignoreDoubleClickEvents', () => { ifdescribe(process.platform === 'darwin')('tray get/set ignoreDoubleClickEvents', () => {
it('returns false by default', () => { it('returns false by default', () => {
const ignored = tray.getIgnoreDoubleClickEvents() const ignored = tray.getIgnoreDoubleClickEvents()