Skip to content

Commit 92f91bf

Browse files
committed
Consolidate API exceptions
1 parent f7307ef commit 92f91bf

File tree

11 files changed

+198
-90
lines changed

11 files changed

+198
-90
lines changed

examples/mnist.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.nio.file.Path;
66

77
import com.github.tadayosi.torchserve.client.impl.DefaultInference;
8-
import com.github.tadayosi.torchserve.client.inference.invoker.ApiException;
8+
import com.github.tadayosi.torchserve.client.model.ApiException;
99

1010
public class mnist {
1111

@@ -21,7 +21,6 @@ public static void main(String... args) throws Exception {
2121
var result1 = inference.predictions(MNIST_MODEL, one);
2222
System.out.println("Answer> " + result1);
2323
} catch (ApiException e) {
24-
System.err.println(e.getResponseBody());
2524
e.printStackTrace();
2625
}
2726
}

examples/register_mnist.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//DEPS com.github.tadayosi.torchserve:torchserve-client:0.1-SNAPSHOT
33

44
import com.github.tadayosi.torchserve.client.impl.DefaultManagement;
5-
import com.github.tadayosi.torchserve.client.management.invoker.ApiException;
5+
import com.github.tadayosi.torchserve.client.model.ApiException;
66
import com.github.tadayosi.torchserve.client.model.RegisterModelOptions;
77
import com.github.tadayosi.torchserve.client.model.SetAutoScaleOptions;
88

@@ -22,7 +22,7 @@ public static void main(String... args) throws Exception {
2222
.build());
2323
System.out.println("setAutoScale> " + response.getStatus());
2424
} catch (ApiException e) {
25-
System.err.println(e.getResponseBody());
25+
e.printStackTrace();
2626
}
2727
}
2828
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.tadayosi.torchserve.client;
22

3-
import com.github.tadayosi.torchserve.client.model.API;
3+
import com.github.tadayosi.torchserve.client.model.Api;
4+
import com.github.tadayosi.torchserve.client.model.ApiException;
45
import com.github.tadayosi.torchserve.client.model.Response;
56

67
/**
@@ -11,26 +12,26 @@ public interface Inference {
1112
/**
1213
* Get openapi description.
1314
*/
14-
API apiDescription() throws Exception;
15+
Api apiDescription() throws ApiException;
1516

1617
/**
1718
* Get TorchServe status.
1819
*/
19-
Response ping() throws Exception;
20+
Response ping() throws ApiException;
2021

2122
/**
2223
* Predictions entry point to get inference using default model version.
2324
*/
24-
Object predictions(String modelName, Object body) throws Exception;
25+
Object predictions(String modelName, Object body) throws ApiException;
2526

2627
/**
2728
* Predictions entry point to get inference using specific model version.
2829
*/
29-
Object predictions(String modelName, String modelVersion, Object body) throws Exception;
30+
Object predictions(String modelName, String modelVersion, Object body) throws ApiException;
3031

3132
/**
3233
* Not supported yet.
3334
*/
34-
Object explanations(String modelName) throws Exception;
35+
Object explanations(String modelName) throws ApiException;
3536

3637
}

src/main/java/com/github/tadayosi/torchserve/client/Management.java

+13-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import java.util.List;
44

5-
import com.github.tadayosi.torchserve.client.model.API;
5+
import com.github.tadayosi.torchserve.client.model.Api;
6+
import com.github.tadayosi.torchserve.client.model.ApiException;
67
import com.github.tadayosi.torchserve.client.model.ModelDetail;
78
import com.github.tadayosi.torchserve.client.model.ModelList;
89
import com.github.tadayosi.torchserve.client.model.RegisterModelOptions;
@@ -18,56 +19,56 @@ public interface Management {
1819
/**
1920
* Register a new model in TorchServe.
2021
*/
21-
Response registerModel(String url, RegisterModelOptions options) throws Exception;
22+
Response registerModel(String url, RegisterModelOptions options) throws ApiException;
2223

2324
/**
2425
* Configure number of workers for a default version of a model. This is an asynchronous call by default. Caller need to call describeModel to check if the model workers has been changed.
2526
*/
26-
Response setAutoScale(String modelName, SetAutoScaleOptions options) throws Exception;
27+
Response setAutoScale(String modelName, SetAutoScaleOptions options) throws ApiException;
2728

2829
/**
2930
* Configure number of workers for a specified version of a model. This is an asynchronous call by default. Caller need to call describeModel to check if the model workers has been changed.
3031
*/
31-
Response setAutoScale(String modelName, String modelVersion, SetAutoScaleOptions options) throws Exception;
32+
Response setAutoScale(String modelName, String modelVersion, SetAutoScaleOptions options) throws ApiException;
3233

3334
/**
3435
* Provides detailed information about the default version of a model.
3536
*/
36-
List<ModelDetail> describeModel(String modelName) throws Exception;
37+
List<ModelDetail> describeModel(String modelName) throws ApiException;
3738

3839
/**
3940
* Provides detailed information about the specified version of a model.If "all" is specified as version, returns the details about all the versions of the model.
4041
*/
41-
List<ModelDetail> describeModel(String modelName, String modelVersion) throws Exception;
42+
List<ModelDetail> describeModel(String modelName, String modelVersion) throws ApiException;
4243

4344
/**
4445
* Unregister the default version of a model from TorchServe if it is the only version available. This is an asynchronous call by default. Caller can call listModels to confirm model is unregistered.
4546
*/
46-
Response unregisterModel(String modelName, UnregisterModelOptions options) throws Exception;
47+
Response unregisterModel(String modelName, UnregisterModelOptions options) throws ApiException;
4748

4849
/**
4950
* Unregister the specified version of a model from TorchServe. This is an asynchronous call by default. Caller can call listModels to confirm model is unregistered.
5051
*/
51-
Response unregisterModel(String modelName, String modelVersion, UnregisterModelOptions options) throws Exception;
52+
Response unregisterModel(String modelName, String modelVersion, UnregisterModelOptions options) throws ApiException;
5253

5354
/**
5455
* List registered models in TorchServe.
5556
*/
56-
ModelList listModels(Integer limit, String nextPageToken) throws Exception;
57+
ModelList listModels(Integer limit, String nextPageToken) throws ApiException;
5758

5859
/**
5960
* Set default version of a model.
6061
*/
61-
Response setDefault(String modelName, String modelVersion) throws Exception;
62+
Response setDefault(String modelName, String modelVersion) throws ApiException;
6263

6364
/**
6465
* Get openapi description.
6566
*/
66-
API apiDescription() throws Exception;
67+
Api apiDescription() throws ApiException;
6768

6869
/**
6970
* Not supported yet.
7071
*/
71-
Object token(String type) throws Exception;
72+
Object token(String type) throws ApiException;
7273

7374
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.tadayosi.torchserve.client;
22

3+
import com.github.tadayosi.torchserve.client.model.ApiException;
4+
35
/**
46
* Metrics API
57
*/
@@ -8,11 +10,11 @@ public interface Metrics {
810
/**
911
* Get TorchServe application metrics in prometheus format.
1012
*/
11-
String metrics() throws Exception;
13+
String metrics() throws ApiException;
1214

1315
/**
1416
* Get TorchServe application metrics in prometheus format.
1517
*/
16-
String metrics(String name) throws Exception;
18+
String metrics(String name) throws ApiException;
1719

1820
}

src/main/java/com/github/tadayosi/torchserve/client/impl/DefaultInference.java

+26-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import com.github.tadayosi.torchserve.client.Inference;
44
import com.github.tadayosi.torchserve.client.inference.api.DefaultApi;
55
import com.github.tadayosi.torchserve.client.inference.invoker.ApiClient;
6-
import com.github.tadayosi.torchserve.client.model.API;
6+
import com.github.tadayosi.torchserve.client.model.Api;
7+
import com.github.tadayosi.torchserve.client.model.ApiException;
78
import com.github.tadayosi.torchserve.client.model.Response;
89

910
public class DefaultInference implements Inference {
@@ -20,23 +21,39 @@ public DefaultInference(int port) {
2021
}
2122

2223
@Override
23-
public API apiDescription() throws Exception {
24-
return API.from(api.apiDescription());
24+
public Api apiDescription() throws ApiException {
25+
try {
26+
return Api.from(api.apiDescription());
27+
} catch (com.github.tadayosi.torchserve.client.inference.invoker.ApiException e) {
28+
throw new ApiException(e);
29+
}
2530
}
2631

2732
@Override
28-
public Response ping() throws Exception {
29-
return Response.from(api.ping());
33+
public Response ping() throws ApiException {
34+
try {
35+
return Response.from(api.ping());
36+
} catch (com.github.tadayosi.torchserve.client.inference.invoker.ApiException e) {
37+
throw new ApiException(e);
38+
}
3039
}
3140

3241
@Override
33-
public Object predictions(String modelName, Object body) throws Exception {
34-
return api.predictions(body, modelName);
42+
public Object predictions(String modelName, Object body) throws ApiException {
43+
try {
44+
return api.predictions(body, modelName);
45+
} catch (com.github.tadayosi.torchserve.client.inference.invoker.ApiException e) {
46+
throw new ApiException(e);
47+
}
3548
}
3649

3750
@Override
38-
public Object predictions(String modelName, String modelVersion, Object body) throws Exception {
39-
return api.versionPredictions(body, modelName, modelVersion);
51+
public Object predictions(String modelName, String modelVersion, Object body) throws ApiException {
52+
try {
53+
return api.versionPredictions(body, modelName, modelVersion);
54+
} catch (com.github.tadayosi.torchserve.client.inference.invoker.ApiException e) {
55+
throw new ApiException(e);
56+
}
4057
}
4158

4259
@Override

0 commit comments

Comments
 (0)