Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SafeLoggable exception for invalid UUID values #2212

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-2212.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: SafeLoggable exception for invalid UUID values
links:
- https://github.com/palantir/conjure-java/pull/2212

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import com.palantir.conjure.visitor.TypeVisitor;
import com.palantir.logsafe.Preconditions;
import com.palantir.logsafe.Safe;
import com.palantir.logsafe.UnsafeArg;
import com.palantir.logsafe.exceptions.SafeIllegalArgumentException;
import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
Expand Down Expand Up @@ -385,7 +387,14 @@ private static CodeBlock valueOfFactoryMethodForPrimitive(PrimitiveType primitiv
.build();
case UUID:
return CodeBlock.builder()
.beginControlFlow("try")
.addStatement("return of($T.fromString(value))", aliasTypeName.withoutAnnotations())
.nextControlFlow("catch (IllegalArgumentException e)")
.addStatement(
"throw new $T(\"Unable to parse as UUID\", e, $T.of(\"input\", value))",
SafeIllegalArgumentException.class,
UnsafeArg.class)
.endControlFlow()
.build();
case DATETIME:
return CodeBlock.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@

package com.palantir.conjure.java.types;

import static com.palantir.logsafe.testing.Assertions.assertThatLoggableExceptionThrownBy;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.palantir.conjure.java.serialization.ObjectMappers;
import com.palantir.logsafe.UnsafeArg;
import com.palantir.logsafe.exceptions.SafeIllegalArgumentException;
import com.palantir.logsafe.exceptions.SafeNullPointerException;
import com.palantir.product.DoubleAliasExample;
import com.palantir.product.ExternalLongAliasOne;
import com.palantir.product.ExternalLongAliasTwo;
import com.palantir.product.UuidAliasExample;
import java.util.UUID;
import org.junit.jupiter.api.Test;

public class AliasTests {
Expand All @@ -39,6 +43,20 @@ public void testNullValueSafeLoggable() {
.hasMessage("value cannot be null");
}

@Test
void testValueOf_validUuid_succeeds() {
UUID uuid = UUID.randomUUID();
assertThat(UuidAliasExample.valueOf(uuid.toString())).isEqualTo(UuidAliasExample.of(uuid));
}

@Test
void testValueOf_invalidUuid_throwsSafeException() {
assertThatLoggableExceptionThrownBy(() -> UuidAliasExample.valueOf("not-a-uuid"))
.isInstanceOf(SafeIllegalArgumentException.class)
.hasMessageContaining("Unable to parse as UUID")
.hasExactlyArgs(UnsafeArg.of("input", "not-a-uuid"));
}

@Test
public void testValueOf_external() {
assertThat(ExternalLongAliasOne.valueOf("123")).isEqualTo(ExternalLongAliasOne.of(123L));
Expand Down