Question: Question:
Put the title and error code in the type that wraps Exception,
When an error occurs in the logic, we are considering a mechanism to divide the processing according to the error code on the logic user application side.
In this case, we are considering managing the error code so that it is not hard-coded in the application used. We are considering the following methods, but if you have any know-how, please let us know.
1. Define as many error codes as Enum type. The error code itself is included in the custom tag.
public enum ErrorType
{
[ErrorEnum("ERROR-0001")]
Error1,
[ErrorEnum("ERROR-0002")]
Error2
}
Error code usage code
※GetCode(Enum)はEnum型からコードを返すメソッドとする。
if(CustomException.ErrorCode == GetCode(ErrorType.Error1))
{
//エラーコードに沿ったエラーメッセージを表示する。
}
else
if(CustomException.ErrorCode == GetCode(ErrorType.Error2))
{
//エラーコードに沿ったエラーメッセージを表示する。
}
2. Create a constant class and define the error code with public const string.
public class ErrorConst
{
public const string Error1Code = "ERROR-0001";
public const string Error2Code = "ERROR-0002";
}
Error code usage code
if(CustomException.ErrorCode == ErrorConst.Error1Code )
{
//エラーコードに沿ったエラーメッセージを表示する。
}
else
if(CustomException.ErrorCode == ErrorConst.Error2Code )
{
//エラーコードに沿ったエラーメッセージを表示する。
}
3. Please let us know if you have any other good methods or precautions for the above methods.
Answer: Answer:
In the first place, I do not feel the need for the "error code" to be a character string, but if it is a simple enum, depending on the version of the reference library (because it may be inserted in between), it is useless if it is treated as a numerical comparison. Is not it?
The pattern of 1 just seems to complicate the use of Enum.GetName () …
Since there is a fundamental story in Error1 and Error2 that "it is not possible to properly notify what kind of error the name is", it seems more straightforward to define it for each error pattern with enum. It's easy to handle.
Also, if you try to enumerate the definitions in const, it will be expensive (it will be picked up by reflection, right?).
If you want to deal with "ORA-nnnn", you can treat the declared enum as an int (default) type, fix the error code value as an int, and then just "read the value". I don't think the form of comparing strings is very meaningful.
That is ex.ErrorCode == (int) SomeErrorType.Error2.
I think it's easier to dig into a Dictionary when creating a message catalog if you handle it as an enum.