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

Fix not to raise an exception from `(Service|Client)RequestContext.cu… #2962

Merged
merged 2 commits into from Aug 3, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -73,17 +73,14 @@ static ClientRequestContext current() {
* Returns the client-side context of the {@link Request} that is being handled in the current thread.
*
* @return the {@link ClientRequestContext} available in the current thread, or {@code null} if unavailable.
* @throws IllegalStateException if the current context is not a {@link ClientRequestContext}.
*/
@Nullable
static ClientRequestContext currentOrNull() {
final RequestContext ctx = RequestContext.currentOrNull();
if (ctx == null) {
return null;
if (ctx instanceof ClientRequestContext) {
return (ClientRequestContext) ctx;
}
checkState(ctx instanceof ClientRequestContext,
"The current context is not a client-side context: %s", ctx);
return (ClientRequestContext) ctx;
return null;
}

/**
Expand All @@ -104,6 +101,12 @@ static <T> T mapCurrent(
return mapper.apply(ctx);
}

final ServiceRequestContext serviceRequestContext = ServiceRequestContext.currentOrNull();
if (serviceRequestContext != null) {
throw new IllegalStateException("The current context is not a client-side context: " +
serviceRequestContext);
}

if (defaultValueSupplier != null) {
return defaultValueSupplier.get();
}
Expand Down
Expand Up @@ -90,14 +90,7 @@ static ServiceRequestContext currentOrNull() {
return null;
}

final ServiceRequestContext root = ctx.root();
if (root != null) {
return root;
}

throw new IllegalStateException(
"The current context is not a server-side context and does not have a root " +
"which means that the context is not invoked by a server request. ctx: " + ctx);
return ctx.root();
}

/**
Expand All @@ -118,6 +111,14 @@ static <T> T mapCurrent(
return mapper.apply(ctx);
}

final ClientRequestContext clientRequestContext = ClientRequestContext.currentOrNull();
if (clientRequestContext != null) {
throw new IllegalStateException(
"The current context is not a server-side context and does not have a root " +
"which means that the context is not invoked by a server request. ctx: " +
clientRequestContext);
}

if (defaultValueSupplier != null) {
return defaultValueSupplier.get();
}
Expand Down
Expand Up @@ -62,9 +62,7 @@ void currentOrNull() {
assertCurrentCtx(null);

try (SafeCloseable unused = serviceRequestContext().push()) {
assertThatThrownBy(ClientRequestContext::currentOrNull)
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("not a client-side context");
assertThat(ClientRequestContext.currentOrNull()).isNull();
}
}

Expand Down
Expand Up @@ -75,9 +75,7 @@ void currentOrNull() {
assertCurrentCtx(null);

try (SafeCloseable unused = clientRequestContext().push()) {
assertThatThrownBy(ServiceRequestContext::currentOrNull)
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("not a server-side context");
assertThat(ServiceRequestContext.currentOrNull()).isNull();
}
}

Expand Down