|
| 1 | +/* |
| 2 | + * (c) Copyright 2024 Palantir Technologies Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.palantir.gradle.failurereports.exceptions; |
| 18 | + |
| 19 | +import com.palantir.gradle.failurereports.common.FailureReport; |
| 20 | +import com.palantir.gradle.failurereports.common.FailureReporterResources; |
| 21 | +import com.palantir.gradle.failurereports.common.ThrowableResources; |
| 22 | + |
| 23 | +/** |
| 24 | + * An exception type that allows passing an extra logs field which is rendered in the CircleCi failure report. |
| 25 | + * It is useful for surfacing errors derived from a subprocess. |
| 26 | + */ |
| 27 | +public final class ExceptionWithLogs extends FailureReporterException { |
| 28 | + |
| 29 | + private final String logs; |
| 30 | + private final boolean includeStackTrace; |
| 31 | + |
| 32 | + public ExceptionWithLogs(String message, String logs, boolean includeStackTrace) { |
| 33 | + this(message, logs, null, includeStackTrace); |
| 34 | + } |
| 35 | + |
| 36 | + public ExceptionWithLogs(String message, String logs) { |
| 37 | + this(message, logs, null, true); |
| 38 | + } |
| 39 | + |
| 40 | + public ExceptionWithLogs(String message, String logs, Throwable throwable) { |
| 41 | + this(message, logs, throwable, true); |
| 42 | + } |
| 43 | + |
| 44 | + public ExceptionWithLogs(String message, String logs, Throwable throwable, boolean includeStackTrace) { |
| 45 | + super(message, throwable); |
| 46 | + // keeping only the last 100kb of logs to avoid any potential OOM issues if the logs are really large. |
| 47 | + this.logs = FailureReporterResources.keepLastBytesSizeOutput(logs, 100 * 1024); |
| 48 | + this.includeStackTrace = includeStackTrace; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public FailureReport getTaskFailureReport(String taskPath, Throwable initialThrowable) { |
| 53 | + String maybeIncludeStacktrace = |
| 54 | + includeStackTrace ? "\n" + ThrowableResources.formatThrowable(initialThrowable) : ""; |
| 55 | + return FailureReport.builder() |
| 56 | + .header(FailureReporterResources.getTaskErrorHeader(taskPath, getMessage())) |
| 57 | + .clickableSource(taskPath) |
| 58 | + .errorMessage(String.join("\n", getMessage(), logs, maybeIncludeStacktrace)) |
| 59 | + .build(); |
| 60 | + } |
| 61 | +} |
0 commit comments