com.streamsets.pipeline.api.impl.ErrorMessage Maven / Gradle / Ivy
/*
* Copyright contributors to the StreamSets project
* StreamSets Inc., an IBM Company 2024
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.streamsets.pipeline.api.impl;
import com.streamsets.pipeline.api.AntennaDoctorMessage;
import com.streamsets.pipeline.api.ErrorCode;
import com.streamsets.pipeline.api.StageException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
public class ErrorMessage implements LocalizableString {
private static final Object[] NULL_ONE_ARG = {null};
private final String errorCode;
private final LocalizableString errorMessage;
private final long timestamp;
private final boolean prependErrorCode;
private final String stackTrace;
public ErrorMessage(final StageException ex) {
errorCode = ex.getErrorCode().getCode();
timestamp = System.currentTimeMillis();
stackTrace = toStackTrace(ex);
errorMessage = new LocalizableString() {
@Override
public String getNonLocalized() {
return ex.getMessage();
}
@Override
public String getLocalized() {
return ex.getLocalizedMessage();
}
@Override
public String getHumanReadableMessage() {
return Utils.format(ex.getErrorCode().getMessage(), ex.getParams());
}
@Override
public String getTechnicalMessage() {
return Utils.format(ex.getErrorCode().getTechnicalMessage(), ex.getParams());
}
@Override
public String getRemediationSteps() {
return Utils.format(ex.getErrorCode().getRemediationSteps(), ex.getParams());
}
};
prependErrorCode = false;
}
public ErrorMessage(String errorCode, final String nonLocalizedMsg, long timestamp) {
this.errorCode = errorCode;
this.errorMessage = new LocalizableString() {
@Override
public String getNonLocalized() {
return nonLocalizedMsg;
}
@Override
public String getLocalized() {
return nonLocalizedMsg;
}
};
this.timestamp = timestamp;
stackTrace = null;
prependErrorCode = true;
}
public ErrorMessage(List antennaDoctorMessages, ErrorCode errorCode, Object... params) {
this(null, Utils.checkNotNull(errorCode, "errorCode").getClass().getName() + "-bundle", errorCode, params);
}
public ErrorMessage(ErrorCode errorCode, Object... params) {
this(null, Utils.checkNotNull(errorCode, "errorCode").getClass().getName() + "-bundle", errorCode, params);
}
public ErrorMessage(String resourceBundle, ErrorCode errorCode, Object... params) {
this(null, resourceBundle, errorCode, params);
}
public ErrorMessage(List antennaDoctorMessages, String resourceBundle, ErrorCode errorCode, Object... params) {
this.errorCode = Utils.checkNotNull(errorCode, "errorCode").getCode();
if ( params != null && params.length > 0 && params[params.length-1] instanceof Throwable){
stackTrace = toStackTrace((Throwable)params[params.length-1]);
} else {
stackTrace = null;
}
errorMessage = new LocalizableMessage(
errorCode.getClass().getClassLoader(),
resourceBundle,
errorCode,
(params != null) ? params : NULL_ONE_ARG
);
timestamp = System.currentTimeMillis();
prependErrorCode = true;
}
/*
Utility function, accessible from tests
*/
public static String toStackTrace(Throwable e){
StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
return writer.toString();
}
public String getErrorStackTrace(){
return stackTrace;
}
@Override
public String getErrorCode() {
return errorCode;
}
public long getTimestamp() {
return timestamp;
}
@Override
public String getNonLocalized() {
return prependErrorCode ? Utils.format("{} - {}", getErrorCode(), errorMessage.getNonLocalized()) :
errorMessage.getNonLocalized();
}
@Override
public String getLocalized() {
return prependErrorCode ? Utils.format("{} - {}", getErrorCode(), errorMessage.getLocalized()) :
errorMessage.getLocalized();
}
@Override
public String getTechnicalMessage() {
return errorMessage.getTechnicalMessage();
}
@Override
public String getRemediationSteps() {
return errorMessage.getRemediationSteps();
}
@Override
public String getHumanReadableMessage() {
return errorMessage.getHumanReadableMessage();
}
@Override
public String toString() {
return getNonLocalized();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy