use of com.palantir.conjure.java.api.errors.ServiceException in project conjure-java by palantir.
the class ConjureExceptionHandlerTest method handlesServiceException.
@Test
public void handlesServiceException() throws IOException {
exception = new ServiceException(ErrorType.CONFLICT, SafeArg.of("foo", "bar"));
Response response = execute();
assertThat(response.body().string()).contains("{\"errorCode\":\"CONFLICT\"").contains("\"parameters\":{\"foo\":\"bar\"}}");
assertThat(response.code()).isEqualTo(ErrorType.CONFLICT.httpErrorCode());
}
use of com.palantir.conjure.java.api.errors.ServiceException in project conjure-java by palantir.
the class ConjureExceptionHandlerTest method handles401RemoteException.
@Test
public void handles401RemoteException() throws IOException {
SerializableError remoteError = SerializableError.forException(new ServiceException(ErrorType.create(Code.UNAUTHORIZED, "Test:ErrorName"), SafeArg.of("foo", "bar")));
exception = new RemoteException(remoteError, ErrorType.UNAUTHORIZED.httpErrorCode());
Response response = execute();
// Propagates errorInstanceId and does not change error code and name
// Does not propagate args
SerializableError expectedPropagatedError = SerializableError.builder().errorCode(ErrorType.UNAUTHORIZED.code().toString()).errorName("Test:ErrorName").errorInstanceId(remoteError.errorInstanceId()).build();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Encodings.json().serializer(new TypeMarker<SerializableError>() {
}).serialize(expectedPropagatedError, stream);
assertThat(response.body().string()).isEqualTo(stream.toString(StandardCharsets.UTF_8));
assertThat(response.code()).isEqualTo(ErrorType.UNAUTHORIZED.httpErrorCode());
}
use of com.palantir.conjure.java.api.errors.ServiceException in project conjure-java by palantir.
the class ErrorGenerator method generateErrorTypesForNamespace.
private static JavaFile generateErrorTypesForNamespace(TypeMapper typeMapper, String conjurePackage, ErrorNamespace namespace, List<ErrorDefinition> errorTypeDefinitions) {
ClassName className = errorTypesClassName(conjurePackage, namespace);
// Generate ErrorType definitions
List<FieldSpec> fieldSpecs = errorTypeDefinitions.stream().map(errorDef -> {
CodeBlock initializer = CodeBlock.of("ErrorType.create(ErrorType.Code.$L, \"$L:$L\")", errorDef.getCode().get(), namespace.get(), errorDef.getErrorName().getName());
FieldSpec.Builder fieldSpecBuilder = FieldSpec.builder(ClassName.get(ErrorType.class), CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, errorDef.getErrorName().getName()), Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).initializer(initializer);
errorDef.getDocs().ifPresent(docs -> fieldSpecBuilder.addJavadoc(docs.get()));
return fieldSpecBuilder.build();
}).collect(Collectors.toList());
// Generate ServiceException factory methods
List<MethodSpec> methodSpecs = errorTypeDefinitions.stream().flatMap(entry -> {
MethodSpec withoutCause = generateExceptionFactory(typeMapper, entry, false);
MethodSpec withCause = generateExceptionFactory(typeMapper, entry, true);
return Stream.of(withoutCause, withCause);
}).collect(Collectors.toList());
// Generate ServiceException factory check methods
List<MethodSpec> checkMethodSpecs = errorTypeDefinitions.stream().map(entry -> {
String exceptionMethodName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, entry.getErrorName().getName());
String methodName = "throwIf" + entry.getErrorName().getName();
String shouldThrowVar = "shouldThrow";
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(methodName).addModifiers(Modifier.PUBLIC, Modifier.STATIC).addParameter(TypeName.BOOLEAN, shouldThrowVar);
methodBuilder.addJavadoc("Throws a {@link ServiceException} of type $L when {@code $L} is true.\n", entry.getErrorName().getName(), shouldThrowVar);
methodBuilder.addJavadoc("@param $L $L\n", shouldThrowVar, "Cause the method to throw when true");
Streams.concat(entry.getSafeArgs().stream(), entry.getUnsafeArgs().stream()).forEach(arg -> {
methodBuilder.addParameter(typeMapper.getClassName(arg.getType()), arg.getFieldName().get());
methodBuilder.addJavadoc("@param $L $L", arg.getFieldName().get(), StringUtils.appendIfMissing(arg.getDocs().map(Javadoc::render).orElse(""), "\n"));
});
methodBuilder.addCode("if ($L) {", shouldThrowVar);
methodBuilder.addCode("throw $L;", Expressions.localMethodCall(exceptionMethodName, Streams.concat(entry.getSafeArgs().stream(), entry.getUnsafeArgs().stream()).map(arg -> arg.getFieldName().get()).collect(Collectors.toList())));
methodBuilder.addCode("}");
return methodBuilder.build();
}).collect(Collectors.toList());
List<MethodSpec> isRemoteExceptionDefinitions = errorTypeDefinitions.stream().map(entry -> {
String typeName = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, entry.getErrorName().getName());
String methodName = "is" + entry.getErrorName().getName();
return MethodSpec.methodBuilder(methodName).addModifiers(Modifier.PUBLIC, Modifier.STATIC).addParameter(RemoteException.class, REMOTE_EXCEPTION_VAR).returns(TypeName.BOOLEAN).addStatement(Expressions.requireNonNull(REMOTE_EXCEPTION_VAR, "remote exception must not be null")).addStatement("return $N.name().equals($N.getError().errorName())", typeName, REMOTE_EXCEPTION_VAR).addJavadoc("Returns true if the {@link $T} is named $L:$L", RemoteException.class, entry.getNamespace(), entry.getErrorName().getName()).build();
}).collect(Collectors.toList());
TypeSpec.Builder typeBuilder = TypeSpec.classBuilder(className).addMethod(privateConstructor()).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addFields(fieldSpecs).addMethods(methodSpecs).addMethods(checkMethodSpecs).addMethods(isRemoteExceptionDefinitions).addAnnotation(ConjureAnnotations.getConjureGeneratedAnnotation(ErrorGenerator.class));
return JavaFile.builder(conjurePackage, typeBuilder.build()).skipJavaLangImports(true).indent(" ").build();
}
use of com.palantir.conjure.java.api.errors.ServiceException in project conjure-java-runtime-api by palantir.
the class RemoteExceptionAssertTest method testSanity.
@Test
public void testSanity() throws Exception {
ErrorType actualType = ErrorType.FAILED_PRECONDITION;
SerializableError error = SerializableError.forException(new ServiceException(actualType));
Assertions.assertThat(new RemoteException(error, actualType.httpErrorCode())).isGeneratedFromErrorType(actualType);
assertThatThrownBy(() -> Assertions.assertThat(new RemoteException(error, actualType.httpErrorCode() + 1)).isGeneratedFromErrorType(actualType)).isInstanceOf(AssertionError.class).hasMessage("Expected error status to be %s, but found %s", actualType.httpErrorCode(), actualType.httpErrorCode() + 1);
}
use of com.palantir.conjure.java.api.errors.ServiceException in project conjure-java-runtime by palantir.
the class JsonExceptionMapperTest method testExpectedSerializedError.
@Test
public void testExpectedSerializedError() throws Exception {
Response response = mapper.toResponse(new ServiceException(ErrorType.NOT_FOUND));
String entity = objectMapper.writeValueAsString(response.getEntity());
assertThat(entity).contains("\"errorCode\" : \"INVALID_ARGUMENT\"");
assertThat(entity).contains("\"errorName\" : \"Default:InvalidArgument\"");
assertThat(entity).contains("\"errorInstanceId\" : ");
}
Aggregations