Skip to content

Commit

Permalink
#29270 Rollback to CustomGraphQLException as Base Class
Browse files Browse the repository at this point in the history
**Reason for Rollback**:
- The change to use `GraphQLErrorException` as the base class for `PermissionDeniedGraphQLException` caused a violation of the SonarQube rule "Inheritance tree of classes should not be too deep."
- The inheritance tree depth exceeded the allowed limit, leading to maintainability issues flagged by SonarQube.
  • Loading branch information
valentinogiardino committed Aug 12, 2024
1 parent 2529e1b commit 5213a35
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.dotcms.graphql.exception;

import graphql.ErrorType;
import graphql.GraphQLError;
import graphql.GraphQLException;
import graphql.language.SourceLocation;
import java.util.Collections;

import java.util.List;
import java.util.Map;

/**
* Custom exception class for GraphQL errors.
* <p>
* This class extends {@link GraphQLException} and implements {@link GraphQLError},
* allowing for customized error handling in GraphQL applications.
* </p>
* <p>
* By implementing {@link GraphQLError}, this exception can provide additional
* context or metadata about the error, such as locations, extensions, and error types.
* </p>
*/
public class CustomGraphQLException extends RuntimeException implements GraphQLError {

/**
* Constructs a new CustomGraphQLException with the specified detail message.
*
* @param message the detail message explaining the reason for the exception.
*/
public CustomGraphQLException(String message) {
super(message);
}

/**
* Provides additional metadata about the error.
* <p>
* This implementation returns an empty map, but subclasses can override this
* method to include custom extensions that provide more information about the error.
* </p>
*
* @return an empty map by default, which can be overridden by subclasses.
*/
@Override
public Map<String, Object> getExtensions() {
return Collections.emptyMap();
}

/**
* Returns the list of source locations related to the error.
* <p>
* This implementation returns an empty list, but subclasses can override this
* method to include the specific locations in the GraphQL query where the error occurred.
* </p>
*
* @return an empty list by default, which can be overridden by subclasses.
*/
@Override
public List<SourceLocation> getLocations() {
return Collections.emptyList();
}

/**
* Returns the type of the error.
* <p>
* This implementation returns {@code null}, but subclasses can override this
* method to provide a specific {@link ErrorType} for the error.
* </p>
*
* @return {@code null} by default, which can be overridden by subclasses.
*/
@Override
public ErrorType getErrorType() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
package com.dotcms.graphql.exception;

import graphql.GraphqlErrorException;
/**
* Custom exception for permission denial in GraphQL operations.
* This exception is used to indicate that a user does not have the necessary permissions
* to access a specific resource.
*/
public class PermissionDeniedGraphQLException extends CustomGraphQLException {

public class PermissionDeniedGraphQLException extends GraphqlErrorException {
private static final String DEFAULT_MESSAGE = "Permission denied: You do not have permission to access this resource.";

/**
* Constructs a new PermissionDeniedGraphQLException with a specific message.
*
* @param message The detail message to be used.
*/
public PermissionDeniedGraphQLException(String message) {
super(new Builder().message(message));
super(message);
}

/**
* Constructs a new PermissionDeniedGraphQLException with a default message.
*/
public PermissionDeniedGraphQLException() {
super(new Builder().message("Permission denied: You do not have permission to access this resource."));
this(DEFAULT_MESSAGE);
}

}


0 comments on commit 5213a35

Please sign in to comment.