Java Code Examples for com.linecorp.armeria.common.util.Exceptions#peel()

The following examples show how to use com.linecorp.armeria.common.util.Exceptions#peel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequiresRoleDecorator.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
static HttpResponse handleException(ServiceRequestContext ctx, Throwable cause) {
    cause = Exceptions.peel(cause);
    if (cause instanceof RepositoryNotFoundException ||
        cause instanceof ProjectNotFoundException) {
        return HttpApiUtil.newResponse(ctx, HttpStatus.NOT_FOUND, cause);
    } else {
        return Exceptions.throwUnsafely(cause);
    }
}
 
Example 2
Source File: Converter.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
static CentralDogmaException convert(Throwable t) {
    t = Exceptions.peel(t);

    if (t instanceof CentralDogmaException) {
        return (CentralDogmaException) t;
    }

    ErrorCode code = ErrorCode.INTERNAL_SERVER_ERROR;
    if (t instanceof IllegalArgumentException) {
        code = ErrorCode.BAD_REQUEST;
    } else if (t instanceof EntryNotFoundException) {
        code = ErrorCode.ENTRY_NOT_FOUND;
    } else if (t instanceof RevisionNotFoundException) {
        code = ErrorCode.REVISION_NOT_FOUND;
    } else if (t instanceof QueryExecutionException) {
        code = ErrorCode.QUERY_FAILURE;
    } else if (t instanceof RedundantChangeException) {
        code = ErrorCode.REDUNDANT_CHANGE;
    } else if (t instanceof ChangeConflictException) {
        code = ErrorCode.CHANGE_CONFLICT;
    } else if (t instanceof ProjectNotFoundException) {
        code = ErrorCode.PROJECT_NOT_FOUND;
    } else if (t instanceof ProjectExistsException) {
        code = ErrorCode.PROJECT_EXISTS;
    } else if (t instanceof RepositoryNotFoundException) {
        code = ErrorCode.REPOSITORY_NOT_FOUND;
    } else if (t instanceof RepositoryExistsException) {
        code = ErrorCode.REPOSITORY_EXISTS;
    } else if (t instanceof ShuttingDownException) {
        code = ErrorCode.SHUTTING_DOWN;
    }

    final CentralDogmaException cde = new CentralDogmaException(code);
    cde.setMessage(t.toString());
    cde.initCause(t);
    return cde;
}
 
Example 3
Source File: CachingRepository.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
CachingRepository(Repository repo, RepositoryCache cache) {
    this.repo = requireNonNull(repo, "repo");
    this.cache = requireNonNull(cache, "cache");

    try {
        final List<Commit> history = repo.history(Revision.INIT, Revision.INIT, ALL_PATH, 1).join();
        firstCommit = history.get(0);
    } catch (CompletionException e) {
        final Throwable cause = Exceptions.peel(e);
        Throwables.throwIfUnchecked(cause);
        throw new StorageException("failed to retrieve the initial commit", cause);
    }
}
 
Example 4
Source File: ArmeriaWebServer.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void stop() {
    try {
        if (isRunning) {
            server.stop().get();
            isRunning = false;
        }
    } catch (Exception cause) {
        throw new WebServerException("Failed to stop " + ArmeriaWebServer.class.getSimpleName(),
                                     Exceptions.peel(cause));
    }
}
 
Example 5
Source File: AccessLogComponent.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Nullable
private static String handleThrowable(@Nullable Throwable cause) {
    if (cause == null) {
        return null;
    }
    cause = Exceptions.peel(cause);
    final String message = cause.getMessage();
    return message != null ? cause.getClass().getSimpleName() + ": " + message
                           : cause.getClass().getSimpleName();
}
 
Example 6
Source File: Server.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Waits until the result of {@link CompletableFuture} which is completed after the {@link #close()} or
 * {@link #closeAsync()} operation is completed.
 */
public void blockUntilShutdown() throws InterruptedException {
    try {
        whenClosed().get();
    } catch (ExecutionException e) {
        throw new CompletionException(e.toString(), Exceptions.peel(e));
    }
}
 
Example 7
Source File: AnnotatedService.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
public HttpResponse handleException(ServiceRequestContext ctx, HttpRequest req, Throwable cause) {
    final Throwable peeledCause = Exceptions.peel(cause);

    if (Flags.annotatedServiceExceptionVerbosity() == ExceptionVerbosity.ALL &&
        logger.isWarnEnabled()) {
        logger.warn("{} Exception raised by method '{}' in '{}':",
                    ctx, methodName, className, peeledCause);
    }

    for (final ExceptionHandlerFunction func : functions) {
        try {
            final HttpResponse response = func.handleException(ctx, req, peeledCause);
            // Check the return value just in case, then pass this exception to the default handler
            // if it is null.
            if (response == null) {
                break;
            }
            return response;
        } catch (FallthroughException ignore) {
            // Do nothing.
        } catch (Exception e) {
            logger.warn("{} Unexpected exception from an exception handler {}:",
                        ctx, func.getClass().getName(), e);
        }
    }

    return HttpResponse.of(HttpStatus.INTERNAL_SERVER_ERROR);
}
 
Example 8
Source File: Flags.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static void setUseOpenSslAndDumpOpenSslInfo() {
    final boolean useOpenSsl = getBoolean("useOpenSsl", true);
    if (!useOpenSsl) {
        // OpenSSL explicitly disabled
        Flags.useOpenSsl = false;
        dumpOpenSslInfo = false;
        return;
    }
    if (!OpenSsl.isAvailable()) {
        final Throwable cause = Exceptions.peel(OpenSsl.unavailabilityCause());
        logger.info("OpenSSL not available: {}", cause.toString());
        Flags.useOpenSsl = false;
        dumpOpenSslInfo = false;
        return;
    }
    Flags.useOpenSsl = true;
    logger.info("Using OpenSSL: {}, 0x{}", OpenSsl.versionString(),
                Long.toHexString(OpenSsl.version() & 0xFFFFFFFFL));
    dumpOpenSslInfo = getBoolean("dumpOpenSslInfo", false);
    if (dumpOpenSslInfo) {
        final SSLEngine engine = SslContextUtil.createSslContext(
                SslContextBuilder::forClient,
                false,
                ImmutableList.of()).newEngine(ByteBufAllocator.DEFAULT);
        logger.info("All available SSL protocols: {}",
                    ImmutableList.copyOf(engine.getSupportedProtocols()));
        logger.info("Default enabled SSL protocols: {}", SslContextUtil.DEFAULT_PROTOCOLS);
        ReferenceCountUtil.release(engine);
        logger.info("All available SSL ciphers: {}", OpenSsl.availableJavaCipherSuites());
        logger.info("Default enabled SSL ciphers: {}", SslContextUtil.DEFAULT_CIPHERS);
    }
}
 
Example 9
Source File: LegacyCentralDogma.java    From centraldogma with Apache License 2.0 4 votes vote down vote up
private static Throwable convertCause(Throwable cause) {
    final Throwable peeledCause = Exceptions.peel(cause);
    final Throwable convertedCause;

    if (peeledCause instanceof com.linecorp.centraldogma.internal.thrift.CentralDogmaException) {
        final String message = peeledCause.getMessage();
        switch (((com.linecorp.centraldogma.internal.thrift.CentralDogmaException) peeledCause)
                .getErrorCode()) {
            case UNIMPLEMENTED:
                convertedCause = new CentralDogmaException("unimplemented", false);
                break;
            case INTERNAL_SERVER_ERROR:
                convertedCause = new CentralDogmaException("internal server error", false);
                break;
            case BAD_REQUEST:
                convertedCause = new CentralDogmaException("bad request", false);
                break;
            case PROJECT_NOT_FOUND:
                convertedCause = new ProjectNotFoundException(message, false);
                break;
            case PROJECT_EXISTS:
                convertedCause = new ProjectExistsException(message, false);
                break;
            case REPOSITORY_NOT_FOUND:
                convertedCause = new RepositoryNotFoundException(message, false);
                break;
            case REPOSITORY_EXISTS:
                convertedCause = new RepositoryExistsException(message, false);
                break;
            case REVISION_NOT_FOUND:
                convertedCause = new RevisionNotFoundException(message, false);
                break;
            case REVISION_EXISTS:
                convertedCause = new ChangeConflictException(message, false);
                break;
            case ENTRY_NOT_FOUND:
                convertedCause = new EntryNotFoundException(message, false);
                break;
            case REDUNDANT_CHANGE:
                convertedCause = new RedundantChangeException(message, false);
                break;
            case CHANGE_CONFLICT:
                convertedCause = new ChangeConflictException(message, false);
                break;
            case QUERY_FAILURE:
                convertedCause = new QueryExecutionException(message, false);
                break;
            case SHUTTING_DOWN:
                convertedCause = new ShuttingDownException(message, false);
                break;
            default:
                throw new Error();
        }
    } else {
        convertedCause = peeledCause;
    }
    return convertedCause;
}