abhishekagarwal87 commented on code in PR #18487:
URL: https://github.com/apache/druid/pull/18487#discussion_r2330236413
##########
processing/src/main/java/org/apache/druid/common/exception/ErrorResponseTransformStrategy.java:
##########
@@ -28,7 +28,8 @@
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "strategy", defaultImpl =
NoErrorResponseTransformStrategy.class)
@JsonSubTypes(value = {
@JsonSubTypes.Type(name = "none", value =
NoErrorResponseTransformStrategy.class),
- @JsonSubTypes.Type(name = "allowedRegex", value =
AllowedRegexErrorResponseTransformStrategy.class)
+ @JsonSubTypes.Type(name = "allowedRegex", value =
AllowedRegexErrorResponseTransformStrategy.class),
+ @JsonSubTypes.Type(name = "roleBased", value =
PersonaBasedErrorTransformStrategy.class),
Review Comment:
```suggestion
@JsonSubTypes.Type(name = "persona", value =
PersonaBasedErrorTransformStrategy.class),
```
##########
processing/src/main/java/org/apache/druid/common/exception/PersonaBasedErrorTransformStrategy.java:
##########
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.common.exception;
+
+import org.apache.druid.error.DruidException;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.java.util.emitter.EmittingLogger;
+import org.apache.druid.java.util.emitter.service.AlertEvent;
+
+import java.util.UUID;
+import java.util.function.Function;
+
+public class PersonaBasedErrorTransformStrategy implements
ErrorResponseTransformStrategy
Review Comment:
please add some javadocs
##########
processing/src/main/java/org/apache/druid/common/exception/PersonaBasedErrorTransformStrategy.java:
##########
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.common.exception;
+
+import org.apache.druid.error.DruidException;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.java.util.emitter.EmittingLogger;
+import org.apache.druid.java.util.emitter.service.AlertEvent;
+
+import java.util.UUID;
+import java.util.function.Function;
+
+public class PersonaBasedErrorTransformStrategy implements
ErrorResponseTransformStrategy
+{
+ private static final String ERROR_WITH_ID_TEMPLATE = "Could not process the
query, please contact your administrator "
+ + "with Error ID [%s]
if the issue persists.";
+ private static final EmittingLogger LOG = new
EmittingLogger(PersonaBasedErrorTransformStrategy.class);
+
+ public static final PersonaBasedErrorTransformStrategy INSTANCE = new
PersonaBasedErrorTransformStrategy();
+
+ @Override
+ public Exception transformIfNeeded(SanitizableException exception)
+ {
+ if (exception instanceof DruidException) {
+ final DruidException druidException = (DruidException) exception;
+ if (druidException.getTargetPersona() == DruidException.Persona.USER) {
+ return druidException;
+ } else {
+ return exception.sanitize(s -> {
+ final String errorId = UUID.randomUUID().toString();
+ LOG.makeAlert(druidException, StringUtils.format("Error ID: [%s]",
errorId))
+ .addData(druidException.getContext())
+ .severity(AlertEvent.Severity.ANOMALY)
+ .emit();
+ return StringUtils.format(ERROR_WITH_ID_TEMPLATE, errorId);
+ });
Review Comment:
instead of calling this, lets add a new function that generates a new error
message. This method takes an exception and returns a string (not an
exception). The returned string then is returned to the end user. The method
should also take in an optional error id. Query endpoints can set this error id
to the query id itself.
##########
processing/src/main/java/org/apache/druid/error/DruidException.java:
##########
@@ -129,7 +131,7 @@
* introduced DruidException.
*/
@NotThreadSafe
-public class DruidException extends RuntimeException
+public class DruidException extends RuntimeException implements
SanitizableException
Review Comment:
lets revert the changes here. I don't think the whole `SanitizableException`
was well thought out and don't want DruidException to extend it. In an ideal
world, we shouldn't have SanitizableException and just use DruidException
##########
processing/src/main/java/org/apache/druid/common/exception/PersonaBasedErrorTransformStrategy.java:
##########
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.common.exception;
+
+import org.apache.druid.error.DruidException;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.java.util.emitter.EmittingLogger;
+import org.apache.druid.java.util.emitter.service.AlertEvent;
+
+import java.util.UUID;
+import java.util.function.Function;
+
+public class PersonaBasedErrorTransformStrategy implements
ErrorResponseTransformStrategy
+{
+ private static final String ERROR_WITH_ID_TEMPLATE = "Could not process the
query, please contact your administrator "
+ + "with Error ID [%s]
if the issue persists.";
+ private static final EmittingLogger LOG = new
EmittingLogger(PersonaBasedErrorTransformStrategy.class);
+
+ public static final PersonaBasedErrorTransformStrategy INSTANCE = new
PersonaBasedErrorTransformStrategy();
+
+ @Override
+ public Exception transformIfNeeded(SanitizableException exception)
+ {
+ if (exception instanceof DruidException) {
+ final DruidException druidException = (DruidException) exception;
+ if (druidException.getTargetPersona() == DruidException.Persona.USER) {
+ return druidException;
+ } else {
+ return exception.sanitize(s -> {
+ final String errorId = UUID.randomUUID().toString();
+ LOG.makeAlert(druidException, StringUtils.format("Error ID: [%s]",
errorId))
+ .addData(druidException.getContext())
+ .severity(AlertEvent.Severity.ANOMALY)
+ .emit();
+ return StringUtils.format(ERROR_WITH_ID_TEMPLATE, errorId);
+ });
Review Comment:
The returned method should be `Optional<String>`
if empty - a transformation occurred
if not - no transformation
caller can decide based on the return type, what to do.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]