diff --git a/dotCMS/src/main/java/com/dotcms/graphql/exception/CustomGraphQLException.java b/dotCMS/src/main/java/com/dotcms/graphql/exception/CustomGraphQLException.java new file mode 100644 index 000000000000..69e516b67f7c --- /dev/null +++ b/dotCMS/src/main/java/com/dotcms/graphql/exception/CustomGraphQLException.java @@ -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. + *

+ * This class extends {@link GraphQLException} and implements {@link GraphQLError}, + * allowing for customized error handling in GraphQL applications. + *

+ *

+ * By implementing {@link GraphQLError}, this exception can provide additional + * context or metadata about the error, such as locations, extensions, and error types. + *

+ */ +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. + *

+ * This implementation returns an empty map, but subclasses can override this + * method to include custom extensions that provide more information about the error. + *

+ * + * @return an empty map by default, which can be overridden by subclasses. + */ + @Override + public Map getExtensions() { + return Collections.emptyMap(); + } + + /** + * Returns the list of source locations related to the error. + *

+ * 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. + *

+ * + * @return an empty list by default, which can be overridden by subclasses. + */ + @Override + public List getLocations() { + return Collections.emptyList(); + } + + /** + * Returns the type of the error. + *

+ * This implementation returns {@code null}, but subclasses can override this + * method to provide a specific {@link ErrorType} for the error. + *

+ * + * @return {@code null} by default, which can be overridden by subclasses. + */ + @Override + public ErrorType getErrorType() { + return null; + } +} \ No newline at end of file diff --git a/dotCMS/src/main/java/com/dotcms/graphql/exception/PermissionDeniedGraphQLException.java b/dotCMS/src/main/java/com/dotcms/graphql/exception/PermissionDeniedGraphQLException.java index db1825e1d4ed..39973190bd8d 100644 --- a/dotCMS/src/main/java/com/dotcms/graphql/exception/PermissionDeniedGraphQLException.java +++ b/dotCMS/src/main/java/com/dotcms/graphql/exception/PermissionDeniedGraphQLException.java @@ -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); } - } - -