Search in sources :

Example 1 with SafeIllegalArgumentException

use of com.palantir.logsafe.exceptions.SafeIllegalArgumentException in project conjure-java by palantir.

the class SafeExchangeCompletionListenerTest method test_throwsRuntimeException.

@Test
public void test_throwsRuntimeException() {
    Mockito.doThrow(new SafeIllegalArgumentException()).when(action).accept(exchange);
    ExchangeCompletionListener listener = SafeExchangeCompletionListener.of(action);
    listener.exchangeEvent(exchange, nextListener);
    Mockito.verify(action).accept(exchange);
    Mockito.verify(nextListener).proceed();
    Mockito.verifyNoMoreInteractions(nextListener, action);
}
Also used : ExchangeCompletionListener(io.undertow.server.ExchangeCompletionListener) SafeIllegalArgumentException(com.palantir.logsafe.exceptions.SafeIllegalArgumentException) Test(org.junit.jupiter.api.Test)

Example 2 with SafeIllegalArgumentException

use of com.palantir.logsafe.exceptions.SafeIllegalArgumentException in project conjure-java by palantir.

the class SafeExchangeCompletionListenerTest method test_proceedThrowsRuntimeException.

@Test
public void test_proceedThrowsRuntimeException() {
    Mockito.doThrow(new SafeIllegalArgumentException()).when(nextListener).proceed();
    ExchangeCompletionListener listener = SafeExchangeCompletionListener.of(action);
    listener.exchangeEvent(exchange, nextListener);
    Mockito.verify(action).accept(exchange);
    Mockito.verify(nextListener).proceed();
    Mockito.verifyNoMoreInteractions(nextListener, action);
}
Also used : ExchangeCompletionListener(io.undertow.server.ExchangeCompletionListener) SafeIllegalArgumentException(com.palantir.logsafe.exceptions.SafeIllegalArgumentException) Test(org.junit.jupiter.api.Test)

Example 3 with SafeIllegalArgumentException

use of com.palantir.logsafe.exceptions.SafeIllegalArgumentException in project conjure-java-runtime-api by palantir.

the class UserAgents method parseInternal.

private static UserAgent parseInternal(String userAgent, boolean lenient) {
    ImmutableUserAgent.Builder builder = ImmutableUserAgent.builder();
    Matcher matcher = SEGMENT_PATTERN.matcher(userAgent);
    boolean foundFirst = false;
    while (matcher.find()) {
        String name = matcher.group(1);
        String version = matcher.group(2);
        Optional<String> comments = Optional.ofNullable(matcher.group(4));
        if (!foundFirst) {
            // primary
            builder.primary(UserAgent.Agent.of(name, version));
            comments.ifPresent(c -> {
                Map<String, String> parsedComments = parseComments(c);
                if (parsedComments.containsKey("nodeId")) {
                    builder.nodeId(parsedComments.get("nodeId"));
                }
            });
        } else {
            // informational
            builder.addInformational(UserAgent.Agent.of(name, version));
        }
        foundFirst = true;
    }
    if (!foundFirst) {
        if (lenient) {
            if (log.isDebugEnabled()) {
                log.debug("Invalid user agent '{}', falling back to default/unknown agent", SafeArg.of("userAgent", userAgent));
            }
            return builder.primary(UserAgent.Agent.of("unknown", UserAgent.Agent.DEFAULT_VERSION)).build();
        } else {
            throw new SafeIllegalArgumentException("Failed to parse user agent string", SafeArg.of("userAgent", userAgent));
        }
    }
    return builder.build();
}
Also used : Matcher(java.util.regex.Matcher) SafeIllegalArgumentException(com.palantir.logsafe.exceptions.SafeIllegalArgumentException)

Example 4 with SafeIllegalArgumentException

use of com.palantir.logsafe.exceptions.SafeIllegalArgumentException in project atlasdb by palantir.

the class ClusterMetadataUtils method getTableMetadata.

public static TableMetadata getTableMetadata(CqlMetadata metadata, String keyspace, String table) {
    KeyspaceMetadata keyspaceMetadata = metadata.getKeyspaceMetadata(keyspace);
    Optional<TableMetadata> maybeTable = keyspaceMetadata.getTables().stream().filter(tableMetadata -> tableMetadata.getName().equals(table)).collect(MoreCollectors.toOptional());
    return maybeTable.orElseThrow(() -> new SafeIllegalArgumentException("Can't find table", SafeArg.of("keyspace", keyspace), LoggingArgs.tableRef("table", TableReference.fromString(table))));
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) RangeSet(com.google.common.collect.RangeSet) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) HashMap(java.util.HashMap) ImmutableRangeSet.toImmutableRangeSet(com.google.common.collect.ImmutableRangeSet.toImmutableRangeSet) Multimap(com.google.common.collect.Multimap) LightweightOppToken(com.palantir.atlasdb.keyvalue.cassandra.LightweightOppToken) SafeArg(com.palantir.logsafe.SafeArg) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) LoggingArgs(com.palantir.atlasdb.logging.LoggingArgs) TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) TableMetadata(com.datastax.driver.core.TableMetadata) ImmutableSet(com.google.common.collect.ImmutableSet) KeyedStream(com.palantir.common.streams.KeyedStream) Predicate(java.util.function.Predicate) MoreCollectors(com.google.common.collect.MoreCollectors) Collection(java.util.Collection) Range(com.google.common.collect.Range) SafeIllegalArgumentException(com.palantir.logsafe.exceptions.SafeIllegalArgumentException) Set(java.util.Set) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata) TreeMap(java.util.TreeMap) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Preconditions(com.palantir.logsafe.Preconditions) SortedMap(java.util.SortedMap) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata) SafeIllegalArgumentException(com.palantir.logsafe.exceptions.SafeIllegalArgumentException)

Example 5 with SafeIllegalArgumentException

use of com.palantir.logsafe.exceptions.SafeIllegalArgumentException in project atlasdb by palantir.

the class ColumnValueDescription method persistJsonToBytes.

@SuppressWarnings("unchecked")
public byte[] persistJsonToBytes(ClassLoader classLoader, String str) throws ParseException {
    final byte[] bytes;
    if (format == Format.PERSISTABLE) {
        throw new SafeIllegalArgumentException("Tried to pass json into a persistable type.");
    } else if (format == Format.PERSISTER) {
        Persister<?> persister = getPersister();
        if (JsonNode.class == persister.getPersistingClassType()) {
            try {
                JsonNode jsonNode = new ObjectMapper().readValue(str, JsonNode.class);
                return ((Persister<JsonNode>) persister).persistToBytes(jsonNode);
            } catch (IOException e) {
                throw Throwables.throwUncheckedException(e);
            }
        } else {
            throw new SafeIllegalArgumentException("Tried to write json to a Persister that isn't for JsonNode.");
        }
    } else if (format == Format.PROTO) {
        Message.Builder builder = createBuilder(classLoader);
        // This will have issues with base64 blobs
        JsonFormat.merge(str, builder);
        bytes = builder.build().toByteArray();
    } else {
        bytes = type.convertFromString(str);
    }
    return CompressionUtils.compress(bytes, compression);
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) Message(com.google.protobuf.Message) JsonNode(com.fasterxml.jackson.databind.JsonNode) Persister(com.palantir.atlasdb.persist.api.Persister) IOException(java.io.IOException) SafeIllegalArgumentException(com.palantir.logsafe.exceptions.SafeIllegalArgumentException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

SafeIllegalArgumentException (com.palantir.logsafe.exceptions.SafeIllegalArgumentException)30 Test (org.junit.jupiter.api.Test)5 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 Range (com.google.common.collect.Range)3 SafeArg (com.palantir.logsafe.SafeArg)3 SafeIllegalStateException (com.palantir.logsafe.exceptions.SafeIllegalStateException)3 InetSocketAddress (java.net.InetSocketAddress)3 List (java.util.List)3 Optional (java.util.Optional)3 Meter (com.codahale.metrics.Meter)2 KeyspaceMetadata (com.datastax.driver.core.KeyspaceMetadata)2 TableMetadata (com.datastax.driver.core.TableMetadata)2 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)2 ImmutableRangeSet.toImmutableRangeSet (com.google.common.collect.ImmutableRangeSet.toImmutableRangeSet)2 MoreCollectors (com.google.common.collect.MoreCollectors)2 Multimap (com.google.common.collect.Multimap)2 RangeSet (com.google.common.collect.RangeSet)2 TableReference (com.palantir.atlasdb.keyvalue.api.TableReference)2