mirror of
https://gitee.com/pnoker/iot-dc3.git
synced 2026-11-03 03:32:08 +08:00
refactor(common): preserve exception causes
This commit is contained in:
+6
-4
@@ -26,6 +26,7 @@ import io.github.pnoker.common.agentic.entity.model.AttachmentDO;
|
||||
import io.github.pnoker.common.agentic.service.AttachmentService;
|
||||
import io.github.pnoker.common.entity.common.RequestHeader;
|
||||
import io.github.pnoker.common.exception.RequestException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.codec.multipart.FilePart;
|
||||
@@ -41,6 +42,7 @@ import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class AttachmentServiceImpl implements AttachmentService {
|
||||
|
||||
@@ -73,7 +75,7 @@ public class AttachmentServiceImpl implements AttachmentService {
|
||||
.subscribeOn(Schedulers.boundedElastic()))
|
||||
.doOnError(error -> deleteFile(filePath))
|
||||
.onErrorMap(Exception.class, error -> error instanceof RequestException ? error
|
||||
: new RequestException("Attachment file save failed"));
|
||||
: new RequestException("Attachment file save failed", error));
|
||||
}
|
||||
|
||||
private AttachmentBO saveAttachment(String conversationId, FilePart filePart, Path filePath,
|
||||
@@ -143,7 +145,7 @@ public class AttachmentServiceImpl implements AttachmentService {
|
||||
try {
|
||||
Files.createDirectories(directory);
|
||||
} catch (Exception e) {
|
||||
throw new RequestException("Attachment directory create failed");
|
||||
throw new RequestException("Attachment directory create failed: {}", directory, e);
|
||||
}
|
||||
|
||||
Path filePath = directory.resolve(UUID.randomUUID() + "-" + safePathPart(fileName)).normalize();
|
||||
@@ -163,8 +165,8 @@ public class AttachmentServiceImpl implements AttachmentService {
|
||||
private void deleteFile(Path filePath) {
|
||||
try {
|
||||
Files.deleteIfExists(filePath);
|
||||
} catch (Exception ignored) {
|
||||
// best effort cleanup for failed uploads
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to clean up attachment file after upload error: {}", filePath, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -104,7 +104,7 @@ public class OfflineExpiryListener {
|
||||
handleDeviceExpiry(key, lastStatus);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("Offline expiry handling failed for key {}: {}", key, e.getMessage(), e);
|
||||
log.warn("Offline expiry handling failed, key={}", key, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
+3
-2
@@ -194,7 +194,8 @@ public class PointValueServiceImpl implements PointValueService {
|
||||
RepositoryService repositoryService = getFirstRepositoryService();
|
||||
repositoryService.savePointValue(pointValueBO);
|
||||
} catch (Exception e) {
|
||||
log.error("Save point value to error {}", e.getMessage());
|
||||
log.error("Save point value failed, tenantId={}, deviceId={}, pointId={}", pointValueBO.getTenantId(),
|
||||
pointValueBO.getDeviceId(), pointValueBO.getPointId(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -216,7 +217,7 @@ public class PointValueServiceImpl implements PointValueService {
|
||||
repositoryService.savePointValues(splitPointValueBO);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Save point values to error {}", e.getMessage());
|
||||
log.error("Save point values failed, deviceId={}, size={}", deviceId, pointValueBOList.size(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-3
@@ -22,9 +22,9 @@ import io.github.pnoker.common.data.biz.ScheduleForDataService;
|
||||
import io.github.pnoker.common.data.entity.property.PointBatchProperties;
|
||||
import io.github.pnoker.common.data.job.HourlyJobForData;
|
||||
import io.github.pnoker.common.data.job.PointValueJob;
|
||||
import io.github.pnoker.common.exception.ServiceException;
|
||||
import io.github.pnoker.common.quartz.QuartzService;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.quartz.DateBuilder;
|
||||
import org.quartz.SchedulerException;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -36,7 +36,6 @@ import org.springframework.stereotype.Service;
|
||||
* @version 2025.9.0
|
||||
* @since 2022.1.0
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class ScheduleForDataServiceImpl implements ScheduleForDataService {
|
||||
|
||||
@@ -64,7 +63,7 @@ public class ScheduleForDataServiceImpl implements ScheduleForDataService {
|
||||
|
||||
quartzService.startScheduler();
|
||||
} catch (SchedulerException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
throw new ServiceException("Failed to initialize data scheduler", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -180,7 +180,7 @@ public class SystemHealthServiceImpl implements SystemHealthService {
|
||||
drivers = future.get(PROBE_TIMEOUT_MS, TimeUnit.MILLISECONDS);
|
||||
} catch (Exception e) {
|
||||
future.cancel(true);
|
||||
log.warn("Driver summary failed: {}", e.getMessage(), e);
|
||||
log.warn("Driver summary failed, tenantId={}", tenantId, e);
|
||||
return summary;
|
||||
}
|
||||
int online = 0;
|
||||
@@ -209,7 +209,7 @@ public class SystemHealthServiceImpl implements SystemHealthService {
|
||||
devices = future.get(PROBE_TIMEOUT_MS, TimeUnit.MILLISECONDS);
|
||||
} catch (Exception e) {
|
||||
future.cancel(true);
|
||||
log.warn("Device summary failed: {}", e.getMessage(), e);
|
||||
log.warn("Device summary failed, tenantId={}", tenantId, e);
|
||||
return summary;
|
||||
}
|
||||
int online = 0;
|
||||
|
||||
Vendored
+1
-1
@@ -89,7 +89,7 @@ public class LocalCacheService {
|
||||
try {
|
||||
listener.onExpire(key, value.value);
|
||||
} catch (Exception e) {
|
||||
log.warn("Expire listener failed for key {}: {}", key, e.getMessage());
|
||||
log.warn("Expire listener failed, key={}", key, e);
|
||||
}
|
||||
}
|
||||
}).build();
|
||||
|
||||
+8
-4
@@ -104,7 +104,8 @@ public class PointValueServer extends PointValueApiGrpc.PointValueApiImplBase {
|
||||
responseObserver.onNext(response.build());
|
||||
responseObserver.onCompleted();
|
||||
} catch (Exception e) {
|
||||
log.error("PointValueServer.lastValue error: {}", e.getMessage(), e);
|
||||
log.error("PointValueServer.lastValue failed, tenantId={}, deviceId={}, pointId={}", request.getTenantId(),
|
||||
request.getDeviceId(), request.getPointId(), e);
|
||||
responseObserver.onNext(GrpcRPointValueDTO.newBuilder()
|
||||
.setResult(GrpcR.newBuilder()
|
||||
.setOk(false)
|
||||
@@ -136,7 +137,8 @@ public class PointValueServer extends PointValueApiGrpc.PointValueApiImplBase {
|
||||
responseObserver.onNext(response.build());
|
||||
responseObserver.onCompleted();
|
||||
} catch (Exception e) {
|
||||
log.error("PointValueServer.historyValue error: {}", e.getMessage(), e);
|
||||
log.error("PointValueServer.historyValue failed, tenantId={}, deviceId={}, pointId={}, count={}",
|
||||
request.getTenantId(), request.getDeviceId(), request.getPointId(), request.getCount(), e);
|
||||
responseObserver.onNext(GrpcRPointValueStringList.newBuilder()
|
||||
.setResult(GrpcR.newBuilder()
|
||||
.setOk(false)
|
||||
@@ -166,7 +168,8 @@ public class PointValueServer extends PointValueApiGrpc.PointValueApiImplBase {
|
||||
.build());
|
||||
responseObserver.onCompleted();
|
||||
} catch (Exception e) {
|
||||
log.error("PointValueServer.readCommand error: {}", e.getMessage(), e);
|
||||
log.error("PointValueServer.readCommand failed, tenantId={}, deviceId={}, pointId={}",
|
||||
request.getTenantId(), request.getDeviceId(), request.getPointId(), e);
|
||||
responseObserver.onNext(GrpcRBoolean.newBuilder()
|
||||
.setResult(GrpcR.newBuilder()
|
||||
.setOk(false)
|
||||
@@ -198,7 +201,8 @@ public class PointValueServer extends PointValueApiGrpc.PointValueApiImplBase {
|
||||
.build());
|
||||
responseObserver.onCompleted();
|
||||
} catch (Exception e) {
|
||||
log.error("PointValueServer.writeCommand error: {}", e.getMessage(), e);
|
||||
log.error("PointValueServer.writeCommand failed, tenantId={}, deviceId={}, pointId={}",
|
||||
request.getTenantId(), request.getDeviceId(), request.getPointId(), e);
|
||||
responseObserver.onNext(GrpcRBoolean.newBuilder()
|
||||
.setResult(GrpcR.newBuilder()
|
||||
.setOk(false)
|
||||
|
||||
+3
-1
@@ -75,7 +75,9 @@ public class DeviceEventReceiver {
|
||||
}
|
||||
RabbitAckUtil.ack(channel, deliveryTag);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
log.error("Device event consume failed, type={}, deliveryTag={}, routingKey={}",
|
||||
Objects.nonNull(entityDTO) ? entityDTO.getType() : null, deliveryTag,
|
||||
message.getMessageProperties().getReceivedRoutingKey(), e);
|
||||
RabbitAckUtil.nack(channel, deliveryTag, true);
|
||||
}
|
||||
}
|
||||
|
||||
+3
-1
@@ -75,7 +75,9 @@ public class DriverEventReceiver {
|
||||
}
|
||||
RabbitAckUtil.ack(channel, deliveryTag);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
log.error("Driver event consume failed, type={}, deliveryTag={}, routingKey={}",
|
||||
Objects.nonNull(entityDTO) ? entityDTO.getType() : null, deliveryTag,
|
||||
message.getMessageProperties().getReceivedRoutingKey(), e);
|
||||
RabbitAckUtil.nack(channel, deliveryTag, true);
|
||||
}
|
||||
}
|
||||
|
||||
+4
-1
@@ -85,7 +85,10 @@ public class PointValueReceiver {
|
||||
}
|
||||
RabbitAckUtil.ack(channel, deliveryTag);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
log.error("Point value consume failed, deviceId={}, pointId={}, deliveryTag={}, routingKey={}",
|
||||
Objects.nonNull(pointValueBO) ? pointValueBO.getDeviceId() : null,
|
||||
Objects.nonNull(pointValueBO) ? pointValueBO.getPointId() : null,
|
||||
deliveryTag, message.getMessageProperties().getReceivedRoutingKey(), e);
|
||||
RabbitAckUtil.nack(channel, deliveryTag, true);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -49,7 +49,7 @@ public class DriverCustomScheduleJob extends QuartzJobBean {
|
||||
try {
|
||||
driverCustomService.schedule();
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to execute custom schedule job: {}", e.getMessage(), e);
|
||||
log.error("Failed to execute custom schedule job", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -81,7 +81,7 @@ public class DriverReadScheduleJob extends QuartzJobBean {
|
||||
try {
|
||||
driverReadService.read(deviceId, pointId);
|
||||
} catch (Exception e) {
|
||||
log.error("Read device[{}], point[{}] error: {}", deviceId, pointId, e.getMessage(), e);
|
||||
log.error("Driver point read schedule failed, deviceId={}, pointId={}", deviceId, pointId, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+5
-1
@@ -122,7 +122,11 @@ public class MetadataReceiver {
|
||||
}
|
||||
RabbitAckUtil.ack(channel, deliveryTag);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
log.error("Driver metadata consume failed, metadataType={}, operateType={}, id={}, deliveryTag={}, routingKey={}",
|
||||
Objects.nonNull(entityDTO) ? entityDTO.getMetadataType() : null,
|
||||
Objects.nonNull(entityDTO) ? entityDTO.getOperateType() : null,
|
||||
Objects.nonNull(entityDTO) ? entityDTO.getId() : null,
|
||||
deliveryTag, message.getMessageProperties().getReceivedRoutingKey(), e);
|
||||
RabbitAckUtil.nack(channel, deliveryTag, true);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -57,8 +57,8 @@ public class DriverRegisterServiceImpl implements DriverRegisterService {
|
||||
// Register driver with the driver client
|
||||
driverClient.driverRegister(entityBO);
|
||||
} catch (Exception e) {
|
||||
log.error("Driver initialization failed: {}", e.getMessage(), e);
|
||||
throw new ServiceException("Driver initialization failed: {}", e.getMessage());
|
||||
log.error("Driver initialization failed", e);
|
||||
throw new ServiceException("Driver initialization failed: {}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-4
@@ -24,8 +24,8 @@ import io.github.pnoker.common.driver.job.DriverReadScheduleJob;
|
||||
import io.github.pnoker.common.driver.job.DriverStatusScheduleJob;
|
||||
import io.github.pnoker.common.driver.service.DriverScheduleService;
|
||||
import io.github.pnoker.common.exception.CronException;
|
||||
import io.github.pnoker.common.exception.ServiceException;
|
||||
import io.github.pnoker.common.quartz.QuartzService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.quartz.CronExpression;
|
||||
import org.quartz.SchedulerException;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -40,7 +40,6 @@ import java.util.Objects;
|
||||
* @version 2025.9.0
|
||||
* @since 2022.1.0
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class DriverScheduleServiceImpl implements DriverScheduleService {
|
||||
|
||||
@@ -92,8 +91,7 @@ public class DriverScheduleServiceImpl implements DriverScheduleService {
|
||||
// Start the scheduler after all jobs are configured
|
||||
quartzService.startScheduler();
|
||||
} catch (SchedulerException e) {
|
||||
// Log any scheduler initialization errors
|
||||
log.error("Driver schedule initial error: {}", e.getMessage(), e);
|
||||
throw new ServiceException("Failed to initialize driver scheduler", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ public class AddException extends RuntimeException {
|
||||
}
|
||||
|
||||
public AddException(String template, Object... params) {
|
||||
super(ExceptionMessageFormatter.format(template, params));
|
||||
super(ExceptionMessageFormatter.format(template, params), ExceptionMessageFormatter.cause(params));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ public class AssociatedException extends RuntimeException {
|
||||
}
|
||||
|
||||
public AssociatedException(String template, Object... params) {
|
||||
super(ExceptionMessageFormatter.format(template, params));
|
||||
super(ExceptionMessageFormatter.format(template, params), ExceptionMessageFormatter.cause(params));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ public class ConfigException extends RuntimeException {
|
||||
}
|
||||
|
||||
public ConfigException(String template, Object... params) {
|
||||
super(ExceptionMessageFormatter.format(template, params));
|
||||
super(ExceptionMessageFormatter.format(template, params), ExceptionMessageFormatter.cause(params));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ public class ConnectorException extends RuntimeException {
|
||||
}
|
||||
|
||||
public ConnectorException(String template, Object... params) {
|
||||
super(ExceptionMessageFormatter.format(template, params));
|
||||
super(ExceptionMessageFormatter.format(template, params), ExceptionMessageFormatter.cause(params));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -35,7 +35,7 @@ public class CronException extends RuntimeException {
|
||||
}
|
||||
|
||||
public CronException(String template, Object... params) {
|
||||
super(ExceptionMessageFormatter.format(template, params));
|
||||
super(ExceptionMessageFormatter.format(template, params), ExceptionMessageFormatter.cause(params));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ public class DeleteException extends RuntimeException {
|
||||
}
|
||||
|
||||
public DeleteException(String template, Object... params) {
|
||||
super(ExceptionMessageFormatter.format(template, params));
|
||||
super(ExceptionMessageFormatter.format(template, params), ExceptionMessageFormatter.cause(params));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ public class DuplicateException extends RuntimeException {
|
||||
}
|
||||
|
||||
public DuplicateException(String template, Object... params) {
|
||||
super(ExceptionMessageFormatter.format(template, params));
|
||||
super(ExceptionMessageFormatter.format(template, params), ExceptionMessageFormatter.cause(params));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ public class EmptyException extends RuntimeException {
|
||||
}
|
||||
|
||||
public EmptyException(String template, Object... params) {
|
||||
super(ExceptionMessageFormatter.format(template, params));
|
||||
super(ExceptionMessageFormatter.format(template, params), ExceptionMessageFormatter.cause(params));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+14
-2
@@ -37,10 +37,11 @@ final class ExceptionMessageFormatter {
|
||||
return template;
|
||||
}
|
||||
|
||||
StringBuilder builder = new StringBuilder(template.length() + params.length * 16);
|
||||
int paramLimit = formatParamLimit(params);
|
||||
StringBuilder builder = new StringBuilder(template.length() + paramLimit * 16);
|
||||
int searchIndex = 0;
|
||||
int paramIndex = 0;
|
||||
while (paramIndex < params.length) {
|
||||
while (paramIndex < paramLimit) {
|
||||
int placeholderIndex = template.indexOf("{}", searchIndex);
|
||||
if (placeholderIndex < 0) {
|
||||
break;
|
||||
@@ -53,4 +54,15 @@ final class ExceptionMessageFormatter {
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
static Throwable cause(Object... params) {
|
||||
if (Objects.isNull(params) || params.length == 0 || !(params[params.length - 1] instanceof Throwable cause)) {
|
||||
return null;
|
||||
}
|
||||
return cause;
|
||||
}
|
||||
|
||||
private static int formatParamLimit(Object... params) {
|
||||
return Objects.nonNull(cause(params)) ? params.length - 1 : params.length;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ public class ImportException extends RuntimeException {
|
||||
}
|
||||
|
||||
public ImportException(String template, Object... params) {
|
||||
super(ExceptionMessageFormatter.format(template, params));
|
||||
super(ExceptionMessageFormatter.format(template, params), ExceptionMessageFormatter.cause(params));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -35,7 +35,7 @@ public class JsonException extends RuntimeException {
|
||||
}
|
||||
|
||||
public JsonException(String template, Object... params) {
|
||||
super(ExceptionMessageFormatter.format(template, params));
|
||||
super(ExceptionMessageFormatter.format(template, params), ExceptionMessageFormatter.cause(params));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -35,7 +35,7 @@ public class NotFoundException extends RuntimeException {
|
||||
}
|
||||
|
||||
public NotFoundException(String template, Object... params) {
|
||||
super(ExceptionMessageFormatter.format(template, params));
|
||||
super(ExceptionMessageFormatter.format(template, params), ExceptionMessageFormatter.cause(params));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ public class OutRangeException extends RuntimeException {
|
||||
}
|
||||
|
||||
public OutRangeException(String template, Object... params) {
|
||||
super(ExceptionMessageFormatter.format(template, params));
|
||||
super(ExceptionMessageFormatter.format(template, params), ExceptionMessageFormatter.cause(params));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ public class ReadPointException extends RuntimeException {
|
||||
}
|
||||
|
||||
public ReadPointException(String template, Object... params) {
|
||||
super(ExceptionMessageFormatter.format(template, params));
|
||||
super(ExceptionMessageFormatter.format(template, params), ExceptionMessageFormatter.cause(params));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ public class RegisterException extends RuntimeException {
|
||||
}
|
||||
|
||||
public RegisterException(String template, Object... params) {
|
||||
super(ExceptionMessageFormatter.format(template, params));
|
||||
super(ExceptionMessageFormatter.format(template, params), ExceptionMessageFormatter.cause(params));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ public class RepositoryException extends RuntimeException {
|
||||
}
|
||||
|
||||
public RepositoryException(String template, Object... params) {
|
||||
super(ExceptionMessageFormatter.format(template, params));
|
||||
super(ExceptionMessageFormatter.format(template, params), ExceptionMessageFormatter.cause(params));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -35,7 +35,7 @@ public class RequestException extends RuntimeException {
|
||||
}
|
||||
|
||||
public RequestException(String template, Object... params) {
|
||||
super(ExceptionMessageFormatter.format(template, params));
|
||||
super(ExceptionMessageFormatter.format(template, params), ExceptionMessageFormatter.cause(params));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ public class SecurityException extends RuntimeException {
|
||||
}
|
||||
|
||||
public SecurityException(String template, Object... params) {
|
||||
super(ExceptionMessageFormatter.format(template, params));
|
||||
super(ExceptionMessageFormatter.format(template, params), ExceptionMessageFormatter.cause(params));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -55,7 +55,7 @@ public class ServiceException extends RuntimeException {
|
||||
* @param params the parameters to format the template
|
||||
*/
|
||||
public ServiceException(String template, Object... params) {
|
||||
super(ExceptionMessageFormatter.format(template, params));
|
||||
super(ExceptionMessageFormatter.format(template, params), ExceptionMessageFormatter.cause(params));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -35,7 +35,7 @@ public class TypeException extends RuntimeException {
|
||||
}
|
||||
|
||||
public TypeException(String template, Object... params) {
|
||||
super(ExceptionMessageFormatter.format(template, params));
|
||||
super(ExceptionMessageFormatter.format(template, params), ExceptionMessageFormatter.cause(params));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ public class UnAuthorizedException extends RuntimeException {
|
||||
}
|
||||
|
||||
public UnAuthorizedException(String template, Object... params) {
|
||||
super(ExceptionMessageFormatter.format(template, params));
|
||||
super(ExceptionMessageFormatter.format(template, params), ExceptionMessageFormatter.cause(params));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ public class UnSupportException extends RuntimeException {
|
||||
}
|
||||
|
||||
public UnSupportException(String template, Object... params) {
|
||||
super(ExceptionMessageFormatter.format(template, params));
|
||||
super(ExceptionMessageFormatter.format(template, params), ExceptionMessageFormatter.cause(params));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ public class UpdateException extends RuntimeException {
|
||||
}
|
||||
|
||||
public UpdateException(String template, Object... params) {
|
||||
super(ExceptionMessageFormatter.format(template, params));
|
||||
super(ExceptionMessageFormatter.format(template, params), ExceptionMessageFormatter.cause(params));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ public class WritePointException extends RuntimeException {
|
||||
}
|
||||
|
||||
public WritePointException(String template, Object... params) {
|
||||
super(ExceptionMessageFormatter.format(template, params));
|
||||
super(ExceptionMessageFormatter.format(template, params), ExceptionMessageFormatter.cause(params));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+10
@@ -84,6 +84,16 @@ class CustomExceptionContractTest {
|
||||
.getDeclaredConstructor(String.class, Object[].class)
|
||||
.newInstance("hello {} number {}", new Object[]{"world", 42});
|
||||
assertThat(exception.getMessage()).isEqualTo("hello world number 42");
|
||||
}),
|
||||
DynamicTest.dynamicTest(
|
||||
exceptionClass.getSimpleName() + " template constructor preserves trailing cause",
|
||||
() -> {
|
||||
Throwable cause = new IllegalStateException("root");
|
||||
RuntimeException exception = exceptionClass
|
||||
.getDeclaredConstructor(String.class, Object[].class)
|
||||
.newInstance("hello {}", new Object[]{"world", cause});
|
||||
assertThat(exception.getMessage()).isEqualTo("hello world");
|
||||
assertThat(exception.getCause()).isSameAs(cause);
|
||||
})));
|
||||
}
|
||||
}
|
||||
|
||||
+14
@@ -32,6 +32,12 @@ class ExceptionMessageFormatterTest {
|
||||
return (String) method.invoke(null, template, params);
|
||||
}
|
||||
|
||||
private Throwable invokeCause(Object... params) throws Exception {
|
||||
Method method = ExceptionMessageFormatter.class.getDeclaredMethod("cause", Object[].class);
|
||||
method.setAccessible(true);
|
||||
return (Throwable) method.invoke(null, new Object[]{params});
|
||||
}
|
||||
|
||||
@Test
|
||||
void returnsTemplateWhenNoPlaceholders() throws Exception {
|
||||
assertThat(invokeFormat("static text", "ignored")).isEqualTo("static text");
|
||||
@@ -76,4 +82,12 @@ class ExceptionMessageFormatterTest {
|
||||
assertThat(invokeFormat("[before:{}:after]", "x"))
|
||||
.isEqualTo("[before:x:after]");
|
||||
}
|
||||
|
||||
@Test
|
||||
void trailingThrowableIsCauseNotFormatParam() throws Exception {
|
||||
IllegalStateException cause = new IllegalStateException("root");
|
||||
assertThat(invokeFormat("operation {} failed", "sync", cause))
|
||||
.isEqualTo("operation sync failed");
|
||||
assertThat(invokeCause("sync", cause)).isSameAs(cause);
|
||||
}
|
||||
}
|
||||
|
||||
+3
-2
@@ -122,8 +122,9 @@ public class ImportDeviceServiceImpl implements ImportDeviceService {
|
||||
entityBO.setDeviceId(deviceBO.getId());
|
||||
entityBO.setTenantId(deviceBO.getTenantId());
|
||||
profileBindService.save(entityBO);
|
||||
} catch (Exception ignored) {
|
||||
// nothing to do
|
||||
} catch (Exception e) {
|
||||
log.warn("Skip profile bind during device import, deviceId={}, profileId={}, error={}",
|
||||
deviceBO.getId(), profileId, e.getMessage(), e);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
+2
-3
@@ -18,11 +18,11 @@
|
||||
package io.github.pnoker.common.manager.biz.impl;
|
||||
|
||||
import io.github.pnoker.common.constant.driver.ScheduleConstant;
|
||||
import io.github.pnoker.common.exception.ServiceException;
|
||||
import io.github.pnoker.common.manager.biz.ScheduleForManagerService;
|
||||
import io.github.pnoker.common.manager.job.HourlyJobForManager;
|
||||
import io.github.pnoker.common.quartz.QuartzService;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.quartz.SchedulerException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -31,7 +31,6 @@ import org.springframework.stereotype.Service;
|
||||
* @version 2025.9.0
|
||||
* @since 2022.1.0
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class ScheduleForManagerServiceImpl implements ScheduleForManagerService {
|
||||
|
||||
@@ -47,7 +46,7 @@ public class ScheduleForManagerServiceImpl implements ScheduleForManagerService
|
||||
|
||||
quartzService.startScheduler();
|
||||
} catch (SchedulerException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
throw new ServiceException("Failed to initialize manager scheduler", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -66,7 +66,7 @@ public class MetadataEventListener implements ApplicationListener<MetadataEvent>
|
||||
entityBOList.forEach(entityBO -> notifyDriver(entityBO.getServiceName(), entityDTO));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Metadata event listener error: {}", e.getMessage(), e);
|
||||
log.error("Metadata event listener failed, event={}", JsonUtil.toJsonString(metadataEvent), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -105,7 +105,8 @@ public class DriverDriverServer extends DriverApiGrpc.DriverApiImplBase {
|
||||
rBuilder.setCode(ResponseEnum.FAILURE.getCode());
|
||||
rBuilder.setMessage(e.getMessage());
|
||||
|
||||
log.error(e.getMessage(), e);
|
||||
log.error("Driver register gRPC request failed, tenant={}, client={}, driver={}", request.getTenant(),
|
||||
request.getClient(), request.getDriver(), e);
|
||||
}
|
||||
|
||||
builder.setResult(rBuilder);
|
||||
|
||||
+6
-5
@@ -315,7 +315,7 @@ public class DeviceServiceImpl implements DeviceService {
|
||||
try {
|
||||
workbook = new XSSFWorkbook(file);
|
||||
} catch (Exception e) {
|
||||
throw new ImportException("The import template file incorrectly: {}", e.getMessage());
|
||||
throw new ImportException("The import template file incorrectly: {}", e.getMessage(), e);
|
||||
}
|
||||
|
||||
if (!configIsEqual(workbook, driverAttributeBOList, pointAttributeBOList, pointBOList)) {
|
||||
@@ -344,7 +344,7 @@ public class DeviceServiceImpl implements DeviceService {
|
||||
try {
|
||||
FileUtils.delete(file);
|
||||
} catch (IOException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
log.error("Failed to delete imported device file: {}", file, e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -512,7 +512,7 @@ public class DeviceServiceImpl implements DeviceService {
|
||||
workbook.write(outputStream);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new ServiceException("Generate template error: {}", e.getMessage());
|
||||
throw new ServiceException("Generate template error: {}", e.getMessage(), e);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
@@ -529,8 +529,9 @@ public class DeviceServiceImpl implements DeviceService {
|
||||
entityBO.setDeviceId(entityDO.getId());
|
||||
entityBO.setTenantId(entityDO.getTenantId());
|
||||
profileBindService.save(entityBO);
|
||||
} catch (Exception ignored) {
|
||||
// nothing to do
|
||||
} catch (Exception e) {
|
||||
log.warn("Skip profile bind during device save, deviceId={}, profileId={}, error={}",
|
||||
entityDO.getId(), profileId, e.getMessage(), e);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
+1
-1
@@ -82,7 +82,7 @@ public class MessageHeader implements Serializable {
|
||||
try {
|
||||
return messageHeaders.get(key, type);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
log.warn("Invalid MQTT message header, key={}, expectedType={}", key, type.getSimpleName(), e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
+1
-1
@@ -94,7 +94,7 @@ public class MqttReceiveHandler {
|
||||
MqttScheduleJob.addMqttMessages(mqttMessage);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
log.error("MQTT inbound dispatch failed, headers={}", message.getHeaders(), e);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
+1
-1
@@ -54,7 +54,7 @@ public class CmdUtil {
|
||||
}
|
||||
process.destroyForcibly();
|
||||
} catch (IOException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
log.error("Failed to destroy process with command: {}", cmd, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -62,7 +62,7 @@ public class FileUtil {
|
||||
try {
|
||||
Files.createDirectories(dir);
|
||||
} catch (IOException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
log.error("Failed to create temp directory: {}", dir, e);
|
||||
}
|
||||
}
|
||||
return dir.toString() + "/";
|
||||
|
||||
+1
-1
@@ -58,7 +58,7 @@ public class HostUtil {
|
||||
InetAddress address = InetAddress.getLocalHost();
|
||||
return address.getHostAddress();
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
log.error("Failed to resolve local host address", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
+1
-1
@@ -98,7 +98,7 @@ public class X509Util {
|
||||
|
||||
return context.getSocketFactory();
|
||||
} catch (Exception e) {
|
||||
throw new ConnectorException(e.getMessage());
|
||||
throw new ConnectorException("Failed to create X509 SSL socket factory: {}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -124,7 +124,7 @@ public class ThreadPoolConfig {
|
||||
runnable.run();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("BlockingRejectedExecutionHandler: {}", e.getMessage(), e);
|
||||
log.error("BlockingRejectedExecutionHandler failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -66,7 +66,7 @@ public class ResponseUtil {
|
||||
|
||||
return ResponseEntity.ok().headers(headers).contentLength(resource.contentLength()).body(resource);
|
||||
} catch (Exception e) {
|
||||
log.error("Error occurred while response file: {}, {}", path.getFileName(), e.getMessage(), e);
|
||||
log.error("Failed to response file: {}", path.getFileName(), e);
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -182,7 +182,8 @@ public class PlcS7DriverCustomServiceImpl implements DriverCustomService {
|
||||
} catch (Exception e) {
|
||||
log.error("Driver connection failed, protocol=plcS7, deviceId={}, host={}, port={}", deviceId, host,
|
||||
port, e);
|
||||
throw new ServiceException("new s7connector fail" + e.getMessage());
|
||||
throw new ServiceException("Driver connection failed, protocol=plcS7, deviceId={}, host={}, port={}",
|
||||
deviceId, host, port, e);
|
||||
}
|
||||
connectMap.put(deviceId, myS7Connector);
|
||||
log.info("Driver connection established, protocol=plcS7, deviceId={}, host={}, port={}", deviceId, host,
|
||||
|
||||
Reference in New Issue
Block a user