Instead of using exceptions extensively, we should use Either as a replacement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
public Result SayHello(string name) { if( name == "hack" ) { throw new Exception("Bad Request"); } return new Result { ErrorMessage = string.Empty }; }
try { return SayHello("xxxx"); } catch (Exception e1) { return new Result { ErrorMessage = e1.Message }; }
This is a C# implementation of the “Either” type. The Either type is a functional programming concept that allows you to represent values that can be one of two types, “left” or “right”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
public Either<Result, Exception> SayHello(string name) { if( name == "hack" ) { return new Either<Result, Exception>(new Exception("Bad Request")); } return new Either<Result, Exception>(new Result { ErrorMessage = $"Hi {name}" }); }
return SayHello("xxx").Match( result => result, ex => new Result { ErrorMessage = string.Empty, } );
var userList = new[] { new MyUser { Path = "", Name = "A1" }, new MyUser { Path = "", Name = "A2" }, new MyUser { Path = "demo", Name = "B1" } };
var actual = _treeBuilder.ReduceTree(userList, x => x.Path, x => _treeBuilder.GetParentPath(x), x => _treeBuilder.QueryParentPaths(x.Path) ).ToList();
var expected = new List<TreeBuilder.TreeItem<MyUser, string>> { new TreeBuilder.TreeItem<MyUser, string>() { Id = "", ParentId = null, Item = new MyUser {Path = "", Name = "A1"}, Children = new List<TreeBuilder.TreeItem<MyUser, string>>() }, new TreeBuilder.TreeItem<MyUser, string>() { Id = "", ParentId = null, Item = new MyUser {Path = "", Name = "A2"}, Children = new List<TreeBuilder.TreeItem<MyUser, string>>() }, new TreeBuilder.TreeItem<MyUser, string>() { Id = "demo", ParentId = null, Item = new MyUser {Path = "demo", Name = "B1"}, Children = new List<TreeBuilder.TreeItem<MyUser, string>>() }, };
var userList = new[] { new MyUser {Path = "", Name = "A1"}, new MyUser {Path = "demo/name", Name = "C1"}, };
var actual = _treeBuilder.GenerateTree(userList, x => x.Path, x => _treeBuilder.GetParentPath(x.Path) ).ToList();
var expected = new List<TreeBuilder.TreeItem<MyUser, string>> { new TreeBuilder.TreeItem<MyUser, string>() { Id = "", ParentId = null, Item = new MyUser {Path = "", Name = "A1"}, Children = new List<TreeBuilder.TreeItem<MyUser, string>>() }, };
// Reserve a global object to avoid JavaScript object GC resulting window automatically closes let mainWindow;
function createWindow () { // Create browser window mainWindow = new BrowserWindow({width: 800, height: 600});
// Load mainWindow.html for view mainWindow.loadURL('file://' + __dirname + '/app/mainWindow.html');
// Open Developer Tools mainWindow.webContents.openDevTools();
// When browser window closed,will send 'closed' signal,and run callback mainWindow.on('closed', function() { mainWindow = null; }); }
// When Electron initialization is completed and begin establish the new window, // will send 'ready' signal,and run callback app.on('ready', createWindow);
// app quit app.on('window-all-closed', function () { // For OSX User platform, force user press Cmd + Q if (process.platform !== 'darwin') { app.quit(); } });
app.on('activate', function () { // For OSX if (mainWindow === null) { createWindow(); } });