Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,11 @@ public class ErrorPayload {

private ErrorDictionary code;

private String subCode;

private String description;

public ErrorPayload(final ErrorDictionary code, final String description) {
this(code, null, description);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import javax.ws.rs.core.Response;

import org.talend.sdk.component.api.exception.ComponentException;
import org.talend.sdk.component.api.exception.ComponentException.ErrorOrigin;
import org.talend.sdk.component.api.exception.DiscoverSchemaException;
import org.talend.sdk.component.runtime.manager.ComponentManager;
import org.talend.sdk.component.runtime.manager.ContainerComponentRegistry;
import org.talend.sdk.component.runtime.manager.ServiceMeta;
Expand Down Expand Up @@ -212,32 +214,42 @@ private CompletableFuture<Response> doExecuteLocalAction(final String family, fi

private Response onError(final Throwable re) {
log.warn(re.getMessage(), re);
if (WebApplicationException.class.isInstance(re.getCause())) {
return WebApplicationException.class.cast(re.getCause()).getResponse();
if (re.getCause() instanceof WebApplicationException webException) {
return webException.getResponse();
}

if (ComponentException.class.isInstance(re)) {
final ComponentException ce = (ComponentException) re;
final String description = "Action execution failed with: " + ofNullable(re.getMessage())
.orElseGet(() -> re instanceof NullPointerException
? "unexpected null"
: "no error message");
if (re instanceof final DiscoverSchemaException eSchema) {
// we send reason to recognize the error on client side
final String subCode = eSchema.getPossibleHandleErrorWith().toString();
throw new WebApplicationException(Response
.status(ce.getErrorOrigin() == ComponentException.ErrorOrigin.USER ? 400
: ce.getErrorOrigin() == ComponentException.ErrorOrigin.BACKEND ? 456 : 520,
"Unexpected callback error")
.entity(new ErrorPayload(ErrorDictionary.ACTION_ERROR,
"Action execution failed with: " + ofNullable(re.getMessage())
.orElseGet(() -> NullPointerException.class.isInstance(re) ? "unexpected null"
: "no error message")))
.status(400, subCode)
.entity(new ErrorPayload(ErrorDictionary.ACTION_ERROR, subCode, description))
.build());
} else if (re instanceof final ComponentException eComponent) {
throw new WebApplicationException(Response
.status(evaluateStatusCodeForException(eComponent), "Unexpected callback error")
.entity(new ErrorPayload(ErrorDictionary.ACTION_ERROR, description))
.build());
}

throw new WebApplicationException(Response
.status(520, "Unexpected callback error")
.entity(new ErrorPayload(ErrorDictionary.ACTION_ERROR,
"Action execution failed with: " + ofNullable(re.getMessage())
.orElseGet(() -> NullPointerException.class.isInstance(re) ? "unexpected null"
: "no error message")))
.entity(new ErrorPayload(ErrorDictionary.ACTION_ERROR, description))
.build());
}

private static int evaluateStatusCodeForException(final ComponentException eComponent) {
return switch (eComponent.getErrorOrigin()) {
case USER -> 400;
case BACKEND -> 456;
default -> 520;
};
}

private Stream<ActionItem> findVirtualActions(final Predicate<String> typeMatcher,
final Predicate<String> componentMatcher, final Locale locale) {
return virtualActions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand All @@ -35,6 +36,7 @@
import javax.inject.Inject;
import javax.json.bind.Jsonb;

import org.talend.sdk.component.api.record.Schema;
import org.talend.sdk.component.runtime.internationalization.ParameterBundle;
import org.talend.sdk.component.runtime.manager.ParameterMeta;
import org.talend.sdk.component.runtime.manager.reflect.parameterenricher.ValidationParameterEnricher;
Expand Down Expand Up @@ -69,54 +71,81 @@

private Stream<SimplePropertyDefinition> buildProperties(final List<ParameterMeta> meta, final ClassLoader loader,
final Locale locale, final DefaultValueInspector.Instance rootInstance, final ParameterMeta parent) {
return meta.stream().flatMap(p -> {
final String path = sanitizePropertyName(p.getPath());
final String name = sanitizePropertyName(p.getName());
final String type = p.getType().name();
final boolean isEnum = p.getType() == ParameterMeta.Type.ENUM;
PropertyValidation validation = propertyValidationService.map(p.getMetadata());
if (isEnum) {
if (validation == null) {
validation = new PropertyValidation();
}
validation.setEnumValues(p.getProposals());
}
final Map<String, String> sanitizedMetadata = ofNullable(p.getMetadata())
.map(m -> m
.entrySet()
.stream()
.filter(e -> !e.getKey().startsWith(ValidationParameterEnricher.META_PREFIX))
.collect(toLinkedMap(e -> e.getKey().replace("tcomp::", ""), Map.Entry::getValue)))
.orElse(null);
final Map<String, String> metadata;
if (parent != null) {
metadata = sanitizedMetadata;
} else {
metadata = ofNullable(sanitizedMetadata).orElseGet(HashMap::new);
metadata.put("definition::parameter::index", String.valueOf(meta.indexOf(p)));
}
final DefaultValueInspector.Instance instance = defaultValueInspector
.createDemoInstance(
ofNullable(rootInstance).map(DefaultValueInspector.Instance::getValue).orElse(null), p);
final ParameterBundle bundle = p.findBundle(loader, locale);
final ParameterBundle parentBundle = parent == null ? null : parent.findBundle(loader, locale);
return Stream
.concat(Stream
.of(new SimplePropertyDefinition(path, name,
bundle.displayName(parentBundle).orElse(p.getName()), type, toDefault(instance, p),
validation, rewriteMetadataForLocale(metadata, parentBundle, bundle),
bundle.placeholder(parentBundle).orElse(p.getName()),
!isEnum ? null
: p
.getProposals()
.stream()
.collect(toLinkedMap(identity(),
key -> bundle
.enumDisplayName(parentBundle, key)
.orElse(key))))),
buildProperties(p.getNestedParameters(), loader, locale, instance, p));
}).sorted(Comparator.comparing(SimplePropertyDefinition::getPath)); // important cause it is the way you want to
// see it
return meta.stream()
.flatMap(p -> buildProperty(p, meta, loader, locale, rootInstance, parent))
// important cause it is the way you want to see it
.sorted(Comparator.comparing(SimplePropertyDefinition::getPath));
}

private Stream<SimplePropertyDefinition> buildProperty(final ParameterMeta p,
final List<ParameterMeta> siblings,
final ClassLoader loader,
final Locale locale,
final DefaultValueInspector.Instance rootInstance,
final ParameterMeta parent) {
final String path = sanitizePropertyName(p.getPath());
final String name = sanitizePropertyName(p.getName());
final String type = p.getType().name();
final PropertyValidation validation = buildValidation(p);
final Map<String, String> metadata = buildMetadata(p, siblings, parent);
final DefaultValueInspector.Instance instance = defaultValueInspector
.createDemoInstance(ofNullable(rootInstance)
.map(DefaultValueInspector.Instance::getValue)
.orElse(null), p);
final ParameterBundle bundle = p.findBundle(loader, locale);
final ParameterBundle parentBundle = parent == null ? null : parent.findBundle(loader, locale);
final String displayName = bundle.displayName(parentBundle).orElse(p.getName());
final String placeholder = bundle.placeholder(parentBundle).orElse(p.getName());
final LinkedHashMap<String, String> enumValues = buildEnumDisplayNames(p, bundle, parentBundle);
final SimplePropertyDefinition def = new SimplePropertyDefinition(path, name, displayName, type,
toDefault(instance, p), validation, rewriteMetadataForLocale(metadata, parentBundle, bundle),
placeholder, enumValues);
return Stream.concat(
Stream.of(def),
buildProperties(p.getNestedParameters(), loader, locale, instance, p));
}

private PropertyValidation buildValidation(final ParameterMeta p) {
PropertyValidation validation = propertyValidationService.map(p.getMetadata());
if (p.getType() != ParameterMeta.Type.ENUM) {
return validation;
}
if (validation == null) {
validation = new PropertyValidation();
}
validation.setEnumValues(p.getProposals());
return validation;
}

private Map<String, String> buildMetadata(final ParameterMeta p, final List<ParameterMeta> siblings,
final ParameterMeta parent) {
final Map<String, String> sanitized = ofNullable(p.getMetadata())
.map(m -> m
.entrySet()
.stream()
.filter(e -> !e.getKey().startsWith(ValidationParameterEnricher.META_PREFIX))
.collect(toLinkedMap(e -> e.getKey().replace("tcomp::", ""), Map.Entry::getValue)))
.orElse(null);
if (parent != null) {
return sanitized;
}

final Map<String, String> metadata = ofNullable(sanitized).orElseGet(HashMap::new);
metadata.put("definition::parameter::index", String.valueOf(siblings.indexOf(p)));
if (p.getJavaType() instanceof Class<?> clazzType && Schema.class.isAssignableFrom(clazzType)) {
metadata.put("definition::parameter::schema", "");
}
return metadata;
}

private LinkedHashMap<String, String> buildEnumDisplayNames(final ParameterMeta p, final ParameterBundle bundle,
final ParameterBundle parentBundle) {
if (p.getType() != ParameterMeta.Type.ENUM) {
return null;

Check warning on line 144 in component-server-parent/component-server/src/main/java/org/talend/sdk/component/server/service/PropertiesService.java

View check run for this annotation

sonar-rnd / SonarQube Code Analysis

component-server-parent/component-server/src/main/java/org/talend/sdk/component/server/service/PropertiesService.java#L144

Return an empty map instead of null.
}
return p.getProposals()
.stream()
.collect(toLinkedMap(identity(), key -> bundle.enumDisplayName(parentBundle, key).orElse(key)));
}

private Map<String, String> rewriteMetadataForLocale(final Map<String, String> metadata,
Expand Down
Loading
Loading