style: 处理错误改为使用异常的方式

go是将错误在函数返回值处体现。其它编程语言是通过try throw catch 的方式。

按照c# 的方式去做,那么捕获异常就成为代码中必不可少的一部分。对于c++
而言,google style 建议不使用 c++ 异常,这是错误的吗?
适用异常会增加运行时开销,这是确定的。对于c++
这种默认内存不安全的语言,异常又增加了内存安全的风险。但现在我是在适用c#
所以放心的使用异常,对于c++代码,使用异常也应该是需要的,内存安全是可以避免的,除非你需要极端的性能
This commit is contained in:
zerlei 2024-07-26 12:49:08 +08:00
parent 1809dbebf5
commit 09c99f4a24
4 changed files with 34 additions and 194 deletions

View file

@ -30,65 +30,4 @@ package webpage [
asp.net --+ remoteserver
asp.net --+ localserver
webpage --+ webtool
```
```plantuml
entity DirInfo {
String ServerPath
String LocalPath
Arr SpecialFiles
Arr ExcludeFiles
}
entity FileInfo {
DateTime mtime
String Path
}
entity ConfigInfo {
Arr<DirInfo> DirInfos
String RemoteAddr
String RemoteName
}
```
```plantuml
allowmixing
skinparam classAttributeIconSize 0
component sqlite
package remoteserver {
class FilesInfoController {
+Arr<FileInfo> GetFilesInfo(DirInfo dirinfo)
+UploadFiles(FileInfo)
}
class SyncLogController {
+GetSyncLog()
}
package SyncPersistence {
}
SyncPersistence --* SyncLogController
SyncPersistence --* FilesInfoController
}
package localserver {
class FilesConfigController {
- GetLocalFilesInfo()
- CompareLocalRemoteFiles()
--
+ SetDirsConfig(Arr<DirInfo> dirs)
}
package ConfigPersistence {
}
ConfigPersistence --* FilesConfigController
}
FilesConfigController <--> FilesInfoController: 文件及信息传递
sqlite --* SyncPersistence
sqlite --* ConfigPersistence
Actor Devloper
Devloper --> FilesConfigController:调用
Devloper --> SyncLogController:查看同步信息
```

View file

@ -126,7 +126,7 @@ public class Dir(string path, List<AFileOrDir>? children = null, NextOpType? nex
/// <param name="IsUpdateObject"> 是否更新Object对象</param>
/// <param name="IsUpdateDirFile">是否更新文件目录树</param>
/// <returns></returns>
public (bool, string) Combine(
public void Combine(
FileDirOp? fileDirOp,
Dir diffdir,
bool IsUpdateObject = true,
@ -135,7 +135,7 @@ public class Dir(string path, List<AFileOrDir>? children = null, NextOpType? nex
{
if (this.FormatedPath != diffdir.FormatedPath)
{
return (false, "their path is not same");
throw new ArgumentException("their path is not same");
}
else
{
@ -215,7 +215,7 @@ public class Dir(string path, List<AFileOrDir>? children = null, NextOpType? nex
{
if (IsUpdateDirFile)
{
fileDirOp?.DirDel(rrdir,false);
fileDirOp?.DirDel(rrdir, false);
}
if (IsUpdateObject)
{
@ -240,84 +240,6 @@ public class Dir(string path, List<AFileOrDir>? children = null, NextOpType? nex
}
}
}
return (true, "");
}
public (bool, string) Combine_Old(Dir other)
{
if (this.FormatedPath != other.FormatedPath)
{
return (false, "their path is not same");
}
else
{
var ldir = this;
var rdir = other;
foreach (var oc in other.Children)
{
if (oc is File rfile)
{
if (rfile.NextOp != null)
{
if (oc.NextOp == NextOpType.Add)
{
ldir.AddChild(new File(rfile.FormatedPath, rfile.MTime));
}
else
{
var n = ldir
.Children.Where(x =>
x.FormatedPath == oc.FormatedPath && x.Type == DirOrFile.File
)
.FirstOrDefault();
if (n is not null)
{
if (oc.NextOp == NextOpType.Del)
{
ldir.Children.Remove(n);
}
else if (oc.NextOp == NextOpType.Modify)
{
if (n is File lfile)
{
lfile.MTime = rfile.MTime;
}
}
}
}
}
}
else if (oc is Dir rrdir)
{
//新增和删除意味着整个文件夹都被新增和删除
if (rrdir.NextOp == NextOpType.Add)
{
ldir.AddChild(rrdir.Clone(null, true));
}
else if (rrdir.NextOp == NextOpType.Del)
{
ldir.Children.RemoveAt(
ldir.Children.FindIndex(x => x.FormatedPath == rrdir.FormatedPath)
);
}
//当子文件夹和文件不确定时
else
{
var n = ldir
.Children.Where(x =>
x.FormatedPath == rrdir.FormatedPath && x.Type == DirOrFile.Dir
)
.FirstOrDefault();
if (n is Dir lldir)
{
lldir.Combine_Old(rrdir);
}
}
}
}
}
return (true, "");
}
/// <summary>
@ -325,9 +247,9 @@ public class Dir(string path, List<AFileOrDir>? children = null, NextOpType? nex
/// </summary>
/// <param name="other">它的一个clone将被合并的dir,它的NextOp 不应该是空,否则什么都不会发生</param>
/// <returns></returns>
public (bool, string) CombineJustObject(Dir other)
public void CombineJustObject(Dir other)
{
return Combine(null, other, true, false);
Combine(null, other, true, false);
}
/// <summary>
@ -335,9 +257,9 @@ public class Dir(string path, List<AFileOrDir>? children = null, NextOpType? nex
/// </summary>
/// <param name="other">它的一个clone将被合并的dir,它的NextOp 不应该是空,否则什么都不会发生</param>
/// <returns></returns>
public (bool, string) CombineJustDirFile(FileDirOp fileDirOp, Dir diffDir)
public void CombineJustDirFile(FileDirOp fileDirOp, Dir diffDir)
{
return Combine(fileDirOp, diffDir, false, true);
Combine(fileDirOp, diffDir, false, true);
}
/// <summary>
@ -345,11 +267,11 @@ public class Dir(string path, List<AFileOrDir>? children = null, NextOpType? nex
/// </summary>
/// <param name="child"></param>
/// <returns></returns>/
protected (bool, string) AddChild(AFileOrDir child)
protected void AddChild(AFileOrDir child)
{
if (child.FormatedPath[..this.FormatedPath.Length] != this.FormatedPath)
{
return (false, "their rootpath are not same!");
throw new ArgumentException("their rootpath are not same!");
}
var filtedChildren = this.Children.Where(x => x.Type == child.Type);
@ -359,7 +281,9 @@ public class Dir(string path, List<AFileOrDir>? children = null, NextOpType? nex
{
if (child is File)
{
return (false, "there are same path in the children");
throw new ArgumentException(
$"there are same path in the children:{child.FormatedPath}"
);
}
else if (child is Dir dir)
{
@ -377,18 +301,17 @@ public class Dir(string path, List<AFileOrDir>? children = null, NextOpType? nex
{
this.Children.Add(child);
}
return (true, "");
}
/// <summary>
/// 从文件目录结构提起文件信息注意此目录文件树不包含文件内容仅有修改时间mtime
/// </summary>
/// <returns></returns>
public (bool, string) ExtractInfo()
public void ExtractInfo()
{
if (this.Children.Count != 0)
{
return (false, "this dir is not empty.");
throw new NotSupportedException("this dir is not empty.");
}
string[] files = Directory.GetFiles(this.FormatedPath);
string[] dirs = Directory.GetDirectories(this.FormatedPath);
@ -402,7 +325,6 @@ public class Dir(string path, List<AFileOrDir>? children = null, NextOpType? nex
ndir.ExtractInfo();
this.Children.Add(ndir);
}
return (true, "");
}
/// <summary>
@ -410,9 +332,9 @@ public class Dir(string path, List<AFileOrDir>? children = null, NextOpType? nex
/// 文件的修改时间,是否修改文件的修改时间,需要定义文件的写入策略 WriteFileStrageFunc
/// </summary>
/// <returns></returns>
public (bool, string) WriteByThisInfo(FileDirOp fileDirOp)
public void WriteByThisInfo(FileDirOp fileDirOp)
{
static (bool, string) f(Dir dir, FileDirOp fileDirOp)
static void f(Dir dir, FileDirOp fileDirOp)
{
foreach (var child in dir.Children)
{
@ -420,11 +342,7 @@ public class Dir(string path, List<AFileOrDir>? children = null, NextOpType? nex
{
if (child is Dir childDir)
{
var (IsSuccess, Message) = fileDirOp.DirCreate(childDir, false);
if (!IsSuccess)
{
return (false, Message);
}
fileDirOp.DirCreate(childDir, false);
f(childDir, fileDirOp);
}
}
@ -432,24 +350,16 @@ public class Dir(string path, List<AFileOrDir>? children = null, NextOpType? nex
{
if (child is File childFile)
{
var (IsSuccess, Message) = fileDirOp.FileCreate(
child.FormatedPath,
childFile.MTime
);
if (!IsSuccess)
{
return (false, Message);
}
fileDirOp.FileCreate(child.FormatedPath, childFile.MTime);
}
else
{
return (false, "child is not File!");
throw new ArgumentException("child is not File!");
}
}
}
return (true, "");
}
return f(this, fileDirOp);
f(this, fileDirOp);
}
/// <summary>

View file

@ -4,18 +4,18 @@ namespace Common;
public abstract class FileDirOp
{
public abstract (bool, string) FileCreate(string absolutePath, DateTime mtime);
public abstract void FileCreate(string absolutePath, DateTime mtime);
public abstract (bool, string) DirCreate(Dir dir, bool IsRecursion = true);
public abstract void DirCreate(Dir dir, bool IsRecursion = true);
public abstract (bool, string) FileModify(string absolutePath, DateTime mtime);
public abstract (bool, string) FileDel(string absolutePath);
public abstract (bool, string) DirDel(Dir dir, bool IsRecursion = true);
public abstract void FileModify(string absolutePath, DateTime mtime);
public abstract void FileDel(string absolutePath);
public abstract void DirDel(Dir dir, bool IsRecursion = true);
}
public class SimpleFileDirOpForTest : FileDirOp
{
public override (bool, string) FileCreate(string absolutePath, DateTime mtime)
public override void FileCreate(string absolutePath, DateTime mtime)
{
using (FileStream fs = System.IO.File.OpenWrite(absolutePath))
{
@ -23,10 +23,9 @@ public class SimpleFileDirOpForTest : FileDirOp
fs.Write(info, 0, info.Length);
}
System.IO.File.SetLastWriteTime(absolutePath, mtime);
return (true, "");
}
public override (bool, string) FileModify(string absolutePath, DateTime mtime)
public override void FileModify(string absolutePath, DateTime mtime)
{
using (FileStream fs = System.IO.File.OpenWrite(absolutePath))
{
@ -34,20 +33,18 @@ public class SimpleFileDirOpForTest : FileDirOp
fs.Write(info, 0, info.Length);
}
System.IO.File.SetLastWriteTime(absolutePath, mtime);
return (true, "");
}
public override (bool, string) FileDel(string absolutePath)
public override void FileDel(string absolutePath)
{
//ToDo 权限检查
if (System.IO.File.Exists(absolutePath))
{
System.IO.File.Delete(absolutePath);
}
return (true, "");
}
public override (bool, string) DirCreate(Dir dir, bool IsRecursion = true)
public override void DirCreate(Dir dir, bool IsRecursion = true)
{
//TODO需做权限检查
if (!Directory.Exists(dir.FormatedPath))
@ -68,10 +65,9 @@ public class SimpleFileDirOpForTest : FileDirOp
}
}
}
return (true, "");
}
public override (bool, string) DirDel(Dir dir, bool IsRecursion = true)
public override void DirDel(Dir dir, bool IsRecursion = true)
{
//TODO 权限检查 正式徐执行递归
if (!IsRecursion)
@ -80,7 +76,6 @@ public class SimpleFileDirOpForTest : FileDirOp
{
Directory.Delete(dir.FormatedPath, true);
}
return (true, "");
}
else
{

View file

@ -28,10 +28,8 @@ public class DirFileOpTest(FilesSeed filesSeed) : IClassFixture<FilesSeed>
[Fact, TestPriority(0)]
public void FileDirWriteExtract()
{
var (IsSuccess, Message) = filesSeed.NewDir.WriteByThisInfo(filesSeed.fileDirOp);
var (IsSuccess2, Message2) = filesSeed.OldDir.WriteByThisInfo(filesSeed.fileDirOp);
Assert.True(IsSuccess, "新文件写入失败!");
Assert.True(IsSuccess2, "旧文件写入失败!");
filesSeed.NewDir.WriteByThisInfo(filesSeed.fileDirOp);
filesSeed.OldDir.WriteByThisInfo(filesSeed.fileDirOp);
Dir nnd = new(filesSeed.NewDir.FormatedPath);
nnd.ExtractInfo();
Assert.True(nnd.IsEqual(filesSeed.NewDir), "新文件提取文件夹的信息与写入信息不一致!");
@ -62,8 +60,7 @@ public class DirFileOpTest(FilesSeed filesSeed) : IClassFixture<FilesSeed>
[Fact, TestPriority(2)]
public void SyncFileDir()
{
var (IsSuccess,Messsage) = filesSeed.OldDir.CombineJustDirFile(filesSeed.fileDirOp,filesSeed.DiffDir);
Assert.True(IsSuccess, "文件更新错误!");
filesSeed.OldDir.CombineJustDirFile(filesSeed.fileDirOp,filesSeed.DiffDir);
Dir oldSync = new(filesSeed.OldDir.FormatedPath);
oldSync.ExtractInfo();
oldSync.ResetRootPath(filesSeed.OldDir.FormatedPath, filesSeed.NewDir.FormatedPath);
@ -76,8 +73,7 @@ public class DirFileOpTest(FilesSeed filesSeed) : IClassFixture<FilesSeed>
[Fact, TestPriority(3)]
public void DirsCombine()
{
var (IsSuccess, Message) = filesSeed.OldDir.CombineJustObject(filesSeed.DiffDir);
Assert.True(IsSuccess, "文件合并出错!");
filesSeed.OldDir.CombineJustObject(filesSeed.DiffDir);
//Assert.False(filesSeed.NewDir.IsEqual(filesSeed.OldDir));
filesSeed.OldDir.ResetRootPath("OldDir", "NewDir");
// Console.WriteLine(filesSeed.OldDir.Path);