Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: improve more friendly exception messages #113

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/main/java/run/halo/s3os/S3ExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package run.halo.s3os;

import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.server.ServerWebInputException;
import software.amazon.awssdk.core.exception.SdkException;
import software.amazon.awssdk.services.s3.model.S3Exception;

@Slf4j
@UtilityClass
public class S3ExceptionHandler {
longjuan marked this conversation as resolved.
Show resolved Hide resolved

/**
* Map user configuration caused S3 exception to ServerWebInputException
* @param throwable Exception
* @return ServerWebInputException or original exception
*/
public static Throwable map(Throwable throwable) {
if (throwable instanceof S3Exception s3e) {
log.error("S3Exception occurred", s3e);
return new ServerWebInputException(String.format(
"The object storage service returned an error status code %d. Please check the storage "
+ "policy configuration and make sure your account and service are working properly.",
s3e.statusCode()));
}
if (throwable instanceof SdkException sdke && sdke.getMessage() != null
&& sdke.getMessage().contains("UnknownHostException")) {
log.error("UnknownHostException occurred", sdke);
return new ServerWebInputException(
"Received an UnknownHostException, please check if the endpoint is entered correctly, "
+ "especially for any spaces before or after the endpoint.");
}
if (throwable instanceof SdkException sdke && sdke.getMessage() != null
&& sdke.getMessage().contains("Connect timed out")) {
log.error("ConnectTimeoutException occurred", sdke);
return new ServerWebInputException(
"Received a ConnectTimeoutException, please check if the endpoint is entered correctly, "
+ "and make sure your object storage service is working properly.");
}
return throwable;
}
}
35 changes: 13 additions & 22 deletions src/main/java/run/halo/s3os/S3LinkServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package run.halo.s3os;

import static run.halo.s3os.S3OsAttachmentHandler.OBJECT_KEY;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这些 import 修改似乎不是预期的。建议统一一下代码格式化规则。例如提供 .editorconfig 文件。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我现在用的是 https://github.com/halo-dev/halo/blob/main/config/checkstyle/checkstyle.xml 的配置文件,之前可能是用远程开发平台没用上配置文件,导致导入顺序不对。


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -9,38 +17,18 @@
import org.springframework.security.core.context.SecurityContext;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.util.UriUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import run.halo.app.core.extension.attachment.Attachment;
import run.halo.app.core.extension.attachment.Constant;
import run.halo.app.core.extension.attachment.Policy;
import run.halo.app.extension.ConfigMap;
import run.halo.app.extension.Metadata;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.infra.utils.JsonUtils;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.S3Configuration;
import software.amazon.awssdk.services.s3.model.HeadObjectRequest;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
import software.amazon.awssdk.services.s3.model.S3Object;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.stream.Collectors;

import static run.halo.s3os.S3OsAttachmentHandler.OBJECT_KEY;


@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -103,7 +91,8 @@ public Mono<S3ListResult> listObjects(String policyName, String continuationToke
listObjectsV2Response.nextContinuationToken(),
listObjectsV2Response.isTruncated()));
});
});
})
.onErrorMap(S3ExceptionHandler::map);
}

@Override
Expand Down Expand Up @@ -155,7 +144,8 @@ public Mono<S3ListResult> listObjectsUnlinked(String policyName, String continua
.getKey() : null, tokenState.currToken,
limitedObjects.size() == pageSize);
});
});
})
.onErrorMap(S3ExceptionHandler::map);
}

record TokenState(String currToken, String nextToken) {
Expand Down Expand Up @@ -200,6 +190,7 @@ public Mono<LinkResult.LinkResultItem> addAttachmentRecord(String policyName,
.flatMap(client::create)
.thenReturn(new LinkResult.LinkResultItem(objectKey, true, null));
}))
.onErrorMap(S3ExceptionHandler::map)
.onErrorResume(throwable ->
Mono.just(new LinkResult.LinkResultItem(objectKey, false, throwable.getMessage())));
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/run/halo/s3os/S3OsAttachmentHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public Mono<Attachment> upload(UploadContext uploadContext) {
final var properties = getProperties(context.configMap());
return upload(context, properties)
.subscribeOn(Schedulers.boundedElastic())
.map(objectDetail -> this.buildAttachment(properties, objectDetail));
.map(objectDetail -> this.buildAttachment(properties, objectDetail))
.onErrorMap(S3ExceptionHandler::map);
});
}

Expand Down Expand Up @@ -116,6 +117,7 @@ public Mono<Attachment> delete(DeleteContext deleteContext) {
})
.thenReturn(context);
})
.onErrorMap(S3ExceptionHandler::map)
.map(DeleteContext::attachment);
}

Expand Down Expand Up @@ -152,7 +154,8 @@ public Mono<URI> getSharedURL(Attachment attachment, Policy policy, ConfigMap co
}
},
SdkPresigner::close)
.subscribeOn(Schedulers.boundedElastic());
.subscribeOn(Schedulers.boundedElastic())
.onErrorMap(S3ExceptionHandler::map);
}

@Override
Expand Down
1 change: 0 additions & 1 deletion src/main/java/run/halo/s3os/S3UnlinkServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package run.halo.s3os;

import java.time.Instant;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand Down
Loading