chore: improve nativeImage path converter error (#21243)
This commit is contained in:
parent
34452ee69e
commit
d20273f95b
2 changed files with 23 additions and 2 deletions
|
@ -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*>(
|
||||||
|
|
|
@ -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()
|
||||||
|
|
Loading…
Reference in a new issue