Skip to content

Conversation

@lizard-boy
Copy link

@lizard-boy lizard-boy commented Oct 19, 2024

Type of change

- [ ] Bug fix
- [ ] New feature development
- [X] Tech debt (refactoring, code cleanup, dependency upgrades, etc)
- [ ] Build/deploy pipeline (DevOps)
- [ ] Other

Objective

This PR introduces centralized error management by creating a class with constant strings for expected exception error codes. It replaces hardcoded error messages with these error code constants, improving maintainability and consistency. Additionally, a resource file maps the error codes to their corresponding messages for easier localization and customization.

Code changes

  • file.ext: Description of what was changed and why

Before you submit

  • Please check for formatting errors (dotnet format --verify-no-changes) (required)
  • If making database changes - make sure you also update Entity Framework queries and/or migrations
  • Please add unit tests where it makes sense to do so (encouraged but not required)
  • If this change requires a documentation update - notify the documentation team
  • If this change has particular deployment requirements - notify the DevOps team

Greptile Summary

This pull request introduces a centralized error management system for the server API, focusing on consistent error handling and improved localization support.

  • Added ErrorCodes static class in src/Core/Constants.cs with constant string fields for various error codes
  • Created ErrorMessages.en.resx resource file in src/Core/Resources/ to map error codes to localized messages
  • Modified ExceptionHandlerFilterAttribute.cs to use the new GetFormattedMessageFromErrorCode method for error message localization
  • Updated PolicyService.cs and SyncController.cs to replace hardcoded error messages with ErrorCodes constants
  • Added ErrorMessages.en.Designer.cs to provide programmatic access to localized error messages

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

7 file(s) reviewed, 15 comment(s)
Edit PR Review Bot Settings | Greptile

public override void OnException(ExceptionContext context)
{
var errorMessage = "An error has occurred.";
var errorMessage = GetFormattedMessageFromErrorCode(context);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider adding a null check for context before calling GetFormattedMessageFromErrorCode

Comment on lines +145 to +146
var errorCode = alternativeErrorCode ?? context.Exception.Message;
var errorMessage = localizer[errorCode];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Using Exception.Message as errorCode could lead to unexpected behavior. Consider using a default error code instead

Comment on lines +149 to +152
if (errorMessage.ResourceNotFound is false)
{
return $"({errorCode}) {localizer[errorCode]}";
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: This logic might return '(ErrorMessage) ErrorMessage' for non-code errors. Consider refactoring to handle this case

return $"({errorCode}) {localizer[errorCode]}";
}

return context.Exception.Message;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Returning Exception.Message as fallback may expose sensitive information. Consider using a generic error message

if (user == null)
{
throw new BadRequestException("User not found.");
throw new BadRequestException(ErrorCodes.UserNotFound);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using NotFoundException instead of BadRequestException for user not found scenario

Comment on lines +50 to +61
public static class ErrorCodes
{
public const string Error = "ERR000";
public const string OrganizationNotFound = "ERR001";
public const string OrganizationCannotUsePolicies = "ERR002";
public const string UserNotFound = "ERR003";
public const string ResourceNotFound = "ERR004";
public const string InvalidToken = "ERR005";
public const string Unauthorized = "ERR006";
public const string PolicyRequiredByTrustedDeviceEncryption = "ERR007";
public const string UnhandledError = "ERR500";
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider adding a comment explaining the purpose of this class and how it should be used


public static class ErrorCodes
{
public const string Error = "ERR000";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: ERR000 seems too generic. Consider using a more specific name like 'GenericError' or 'UnspecifiedError'

[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
internal static System.Resources.ResourceManager ResourceManager {
get {
if (object.Equals(null, resourceMan)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using 'resourceMan == null' instead of 'object.Equals(null, resourceMan)' for better readability and performance

Comment on lines +66 to +69
internal static string ERR000 {
get {
return ResourceManager.GetString("ERR000", resourceCulture);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: ERR000 is placed out of order. Consider moving it to be the first error code for consistency

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants