-
Notifications
You must be signed in to change notification settings - Fork 25
Generate C# exception for the project #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -650,6 +650,108 @@ func buildBindingCSharpImplementation(component ComponentDefinition, w LanguageW | |||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln("namespace %s {", NameSpace) | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln("") | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // Generate custom exception class | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" /// <summary>") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" /// Exception class for %s errors", NameSpace) | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" /// </summary>") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" public class E%sException : Exception", NameSpace) | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" {") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" private readonly int _errorCode;") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" private readonly string _errorMessage;") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln("") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" /// <summary>") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" /// Initializes a new instance of the E%sException class", NameSpace) | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" /// </summary>") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" /// <param name=\"errorCode\">The error code</param>") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" /// <param name=\"errorMessage\">The error message</param>") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" public E%sException(int errorCode, string errorMessage = \"\") : base(FormatMessage(errorCode, errorMessage))", NameSpace) | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" {") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" _errorCode = errorCode;") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" _errorMessage = errorMessage;") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" }") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln("") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" /// <summary>") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" /// Gets the error code") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" /// </summary>") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" public int ErrorCode => _errorCode;") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln("") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" /// <summary>") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" /// Gets the custom error message") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" /// </summary>") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" public string ErrorMessage => _errorMessage;") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln("") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" /// <summary>") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" /// Gets the error name (constant name)") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" /// </summary>") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" public string ErrorName") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" {") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" get") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" {") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" switch (_errorCode)") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" {") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" case 0: return \"SUCCESS\";") | ||||||||||||||||||||||||||||||||||||||||||||||
| for _, errorDef := range component.Errors.Errors { | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" case %d: return \"%s\";", errorDef.Code, errorDef.Name) | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" default: return \"UNKNOWN\";") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" }") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" }") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" }") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln("") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" /// <summary>") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" /// Gets the error description (human-readable)") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" /// </summary>") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" public string ErrorDescription") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" {") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" get") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" {") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" switch (_errorCode)") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" {") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" case 0: return \"success\";") | ||||||||||||||||||||||||||||||||||||||||||||||
| for _, errorDef := range component.Errors.Errors { | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" case %d: return \"%s\";", errorDef.Code, errorDef.Description) | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" default: return \"unknown error\";") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" }") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" }") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" }") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln("") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" private static string FormatMessage(int errorCode, string errorMessage)") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" {") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" string errorName = GetErrorName(errorCode);") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" string errorDesc = GetErrorDescription(errorCode);") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" if (!string.IsNullOrEmpty(errorMessage))") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" return $\"E%sException {errorName} ({errorCode}): {errorDesc} - {errorMessage}\";", NameSpace) | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" else") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" return $\"E%sException {errorName} ({errorCode}): {errorDesc}\";", NameSpace) | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" }") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln("") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" private static string GetErrorName(int errorCode)") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" {") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" switch (errorCode)") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" {") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" case 0: return \"SUCCESS\";") | ||||||||||||||||||||||||||||||||||||||||||||||
| for _, errorDef := range component.Errors.Errors { | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" case %d: return \"%s\";", errorDef.Code, errorDef.Name) | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" default: return \"UNKNOWN\";") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" }") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" }") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln("") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" private static string GetErrorDescription(int errorCode)") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" {") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" switch (errorCode)") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" {") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" case 0: return \"success\";") | ||||||||||||||||||||||||||||||||||||||||||||||
| for _, errorDef := range component.Errors.Errors { | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" case %d: return \"%s\";", errorDef.Code, errorDef.Description) | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" default: return \"unknown error\";") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" }") | ||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" }") | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+730
to
+751
|
||||||||||||||||||||||||||||||||||||||||||||||
| w.Writeln(" {") | |
| w.Writeln(" switch (errorCode)") | |
| w.Writeln(" {") | |
| w.Writeln(" case 0: return \"SUCCESS\";") | |
| for _, errorDef := range component.Errors.Errors { | |
| w.Writeln(" case %d: return \"%s\";", errorDef.Code, errorDef.Name) | |
| } | |
| w.Writeln(" default: return \"UNKNOWN\";") | |
| w.Writeln(" }") | |
| w.Writeln(" }") | |
| w.Writeln("") | |
| w.Writeln(" private static string GetErrorDescription(int errorCode)") | |
| w.Writeln(" {") | |
| w.Writeln(" switch (errorCode)") | |
| w.Writeln(" {") | |
| w.Writeln(" case 0: return \"success\";") | |
| for _, errorDef := range component.Errors.Errors { | |
| w.Writeln(" case %d: return \"%s\";", errorDef.Code, errorDef.Description) | |
| } | |
| w.Writeln(" default: return \"unknown error\";") | |
| w.Writeln(" }") | |
| w.Writeln(" }") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The FormatMessage method creates duplicate code by repeating the same switch logic found in ErrorName and ErrorDescription properties. Consider reusing the existing properties instead of calling separate GetErrorName and GetErrorDescription methods.