Skip to content
This repository was archived by the owner on Sep 25, 2023. It is now read-only.

Commit 54ced46

Browse files
committed
feat: Implemented MockServerException
1 parent 66057e7 commit 54ced46

File tree

7 files changed

+46
-9
lines changed

7 files changed

+46
-9
lines changed

src/main/java/io/fabric8/mockwebserver/DefaultMockServer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public void start() {
104104
startInternal();
105105
server.start();
106106
} catch (IOException e) {
107-
throw new RuntimeException(e);
107+
throw new MockServerException("Exception when starting DefaultMockServer", e);
108108
}
109109
}
110110

@@ -113,7 +113,7 @@ public void start(int port) {
113113
startInternal();
114114
server.start(port);
115115
} catch (IOException e) {
116-
throw new RuntimeException(e);
116+
throw new MockServerException("Exception when starting DefaultMockServer with port", e);
117117
}
118118
}
119119

@@ -122,15 +122,15 @@ public void start(InetAddress inetAddress, int port) {
122122
startInternal();
123123
server.start(inetAddress, port);
124124
} catch (IOException e) {
125-
throw new RuntimeException(e);
125+
throw new MockServerException("Exception when starting DefaultMockServer with InetAddress and port", e);
126126
}
127127
}
128128

129129
public void shutdown() {
130130
try {
131131
server.shutdown();
132132
} catch (IOException e) {
133-
throw new RuntimeException(e);
133+
throw new MockServerException("Exception when stopping DefaultMockServer", e);
134134
} finally {
135135
shutdownInternal();
136136
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (C) 2015 Red Hat, Inc.
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+
package io.fabric8.mockwebserver;
17+
18+
public class MockServerException extends RuntimeException {
19+
20+
private static final long serialVersionUID = 2158577731194403856L;
21+
22+
public MockServerException(String message) {
23+
super(message);
24+
}
25+
26+
public MockServerException(String message, Throwable cause) {
27+
super(message, cause);
28+
}
29+
}

src/main/java/io/fabric8/mockwebserver/crud/CrudDispatcher.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.fasterxml.jackson.databind.JsonNode;
1919
import io.fabric8.mockwebserver.Context;
20+
import io.fabric8.mockwebserver.MockServerException;
2021
import io.fabric8.zjsonpatch.JsonPatch;
2122
import okhttp3.mockwebserver.Dispatcher;
2223
import okhttp3.mockwebserver.MockResponse;
@@ -108,7 +109,7 @@ public MockResponse handlePatch(String path, String body) {
108109
response.setResponseCode(202);
109110
response.setBody(updatedAsString);
110111
} catch (Exception e) {
111-
throw new RuntimeException(e);
112+
throw new MockServerException("Exception when handling CRUD patch", e);
112113
}
113114

114115
}

src/main/java/io/fabric8/mockwebserver/internal/InlineWebSocketSessionBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.fasterxml.jackson.core.JsonProcessingException;
2020
import com.fasterxml.jackson.databind.ObjectMapper;
2121
import io.fabric8.mockwebserver.Context;
22+
import io.fabric8.mockwebserver.MockServerException;
2223
import io.fabric8.mockwebserver.dsl.Emitable;
2324
import io.fabric8.mockwebserver.dsl.EventDoneable;
2425
import io.fabric8.mockwebserver.dsl.Function;
@@ -188,7 +189,7 @@ private WebSocketMessage toWebSocketMessage(Long delay, Object content, Boolean
188189
try {
189190
return toWebSocketMessage(delay, MAPPER.writeValueAsString(content), toBeRemoved);
190191
} catch (JsonProcessingException e) {
191-
throw new RuntimeException(e);
192+
throw new MockServerException("Exception when mapping to WebSocketMessage", e);
192193
}
193194
}
194195
}

src/main/java/io/fabric8/mockwebserver/internal/MockSSLContextFactory.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,25 @@
1717
package io.fabric8.mockwebserver.internal;
1818

1919

20+
import io.fabric8.mockwebserver.MockServerException;
2021
import io.fabric8.mockwebserver.utils.SSLUtils;
2122

2223
import javax.net.ssl.KeyManager;
2324
import javax.net.ssl.SSLContext;
2425

2526
public class MockSSLContextFactory {
2627

28+
private MockSSLContextFactory() {
29+
}
30+
2731
public static SSLContext create() {
2832
try {
2933
KeyManager[] keyManagers = SSLUtils.keyManagers(MockSSLContextFactory.class.getResourceAsStream("/ssl/fabric8.crt"),
3034
MockSSLContextFactory.class.getResourceAsStream("/ssl/fabric8"),
3135
"RSA", "");
3236
return SSLUtils.sslContext(keyManagers, null, true);
3337
} catch (Exception e) {
34-
throw new RuntimeException(e);
38+
throw new MockServerException("Exception creating SSLContext", e);
3539
}
3640
}
3741
}

src/main/java/io/fabric8/mockwebserver/internal/MockServerExpectationImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.stream.Stream;
2929

3030
import io.fabric8.mockwebserver.Context;
31+
import io.fabric8.mockwebserver.MockServerException;
3132
import io.fabric8.mockwebserver.ServerRequest;
3233
import io.fabric8.mockwebserver.ServerResponse;
3334
import io.fabric8.mockwebserver.dsl.DelayPathable;
@@ -271,7 +272,7 @@ private String toString(Object object) {
271272
try {
272273
return context.getMapper().writeValueAsString(object);
273274
} catch (JsonProcessingException e) {
274-
throw new RuntimeException(e);
275+
throw new MockServerException("Exception when mapping Object to String", e);
275276
}
276277
}
277278
}

src/main/java/io/fabric8/mockwebserver/internal/WebSocketSession.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.fabric8.mockwebserver.internal;
1818

19+
import io.fabric8.mockwebserver.MockServerException;
1920
import io.fabric8.mockwebserver.dsl.HttpMethod;
2021
import okhttp3.Response;
2122
import io.fabric8.mockwebserver.Context;
@@ -165,7 +166,7 @@ private void checkIfShouldClose() {
165166
}
166167
webSocketRef.get().close(1000, "Closing...");
167168
} catch (Exception ex) {
168-
throw new RuntimeException(ex);
169+
throw new MockServerException("Exception when shutting down WebSocketSession", ex);
169170
}
170171
}
171172
}

0 commit comments

Comments
 (0)