-
Notifications
You must be signed in to change notification settings - Fork 27
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
base: develop
Are you sure you want to change the base?
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The GetErrorName and GetErrorDescription methods duplicate the switch logic already present in the ErrorName and ErrorDescription properties. This creates code duplication and maintenance overhead. These static methods are unnecessary since the instance properties provide the same functionality.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||||||||||||||||||||||
w.Writeln(" }") | ||||||||||||||||||||||||||||||||||||||||||||||
w.Writeln("") | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
for i := 0; i < len(component.Enums); i++ { | ||||||||||||||||||||||||||||||||||||||||||||||
enum := component.Enums[i] | ||||||||||||||||||||||||||||||||||||||||||||||
w.Writeln(" public enum e%s {", enum.Name) | ||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -967,7 +1069,7 @@ func buildBindingCSharpImplementation(component ComponentDefinition, w LanguageW | |||||||||||||||||||||||||||||||||||||||||||||
w.Writeln(" }") | ||||||||||||||||||||||||||||||||||||||||||||||
w.Writeln("") | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
w.Writeln(" throw new Exception(sMessage + \"(# \" + errorCode + \")\");") | ||||||||||||||||||||||||||||||||||||||||||||||
w.Writeln(" throw new E%sException(errorCode, sMessage);", NameSpace) | ||||||||||||||||||||||||||||||||||||||||||||||
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.
Copilot uses AI. Check for mistakes.