[Refactor] Deprecated status in restful response and use code instead (#28206)

Signed-off-by: satanson <ranpanf@gmail.com>
This commit is contained in:
satanson 2023-07-30 10:47:18 +08:00 committed by GitHub
parent 6a7caaba4b
commit 06c5ae2e24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 84 additions and 15 deletions

View File

@ -34,7 +34,6 @@ package com.starrocks.http.rest;
import com.google.common.base.Strings;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.starrocks.analysis.StringLiteral;
@ -138,11 +137,8 @@ public class ExecuteSqlAction extends RestBaseAction {
finalize(request, response, parsedStmt, context);
} catch (StarRocksHttpException e) {
// send {"exception","error message"}
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("exception", e.getMessage());
String exceptionInfo = jsonObject.toString();
response.getContent().append(exceptionInfo);
RestBaseResult failResult = new RestBaseResult(e.getMessage());
response.getContent().append(failResult.toJsonString());
sendResult(request, response, HttpResponseStatus.valueOf(e.getCode().code()));
}
}

View File

@ -34,30 +34,69 @@
package com.starrocks.http.rest;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
// Base restful result
public class RestBaseResult {
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface Legacy {
}
private static final RestBaseResult OK = new RestBaseResult();
// For compatibility, status still exists in /api/v1, removed in /api/v2 and later version.
@Legacy
public ActionStatus status;
public String code;
// For compatibility, msg still exists in /api/v1, removed in /api/v2 and later version.
@Legacy
public String msg;
public String message;
public RestBaseResult() {
status = ActionStatus.OK;
code = "" + ActionStatus.OK.ordinal();
msg = "Success";
message = "OK";
}
public RestBaseResult(String msg) {
status = ActionStatus.FAILED;
code = "" + ActionStatus.FAILED.ordinal();
this.msg = msg;
this.message = msg;
}
public static RestBaseResult getOk() {
return OK;
}
@Legacy
public String toJson() {
Gson gson = new Gson();
return gson.toJson(this);
}
public String toJsonString() {
Gson gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
return f.getAnnotation(Legacy.class) != null;
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
}).create();
return gson.toJson(this);
}
}

View File

@ -49,7 +49,7 @@ public class WarehouseAction extends RestBaseAction {
RestSuccessBaseResult<Result> res = new RestSuccessBaseResult<>(new Result(warehouseInfo.values()));
response.setContentType("application/json");
response.getContent().append(res.toJson());
response.getContent().append(res.toJsonString());
sendResult(request, response);
}

View File

@ -0,0 +1,34 @@
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.starrocks.http;
import com.starrocks.http.rest.RestBaseResult;
import org.junit.Assert;
import org.junit.Test;
public class RestBaseResultTest {
@Test
public void testToJson() {
RestBaseResult result = new RestBaseResult();
Assert.assertEquals(result.toJson(),
"{\"status\":\"OK\",\"code\":\"0\",\"msg\":\"Success\",\"message\":\"OK\"}");
Assert.assertEquals(result.toJsonString(), "{\"code\":\"0\",\"message\":\"OK\"}");
RestBaseResult failedResult = new RestBaseResult("NPE");
Assert.assertEquals(failedResult.toJson(),
"{\"status\":\"FAILED\",\"code\":\"1\",\"msg\":\"NPE\",\"message\":\"NPE\"}");
Assert.assertEquals(failedResult.toJsonString(), "{\"code\":\"1\",\"message\":\"NPE\"}");
}
}

View File

@ -125,7 +125,7 @@ sync;
shell: curl -X POST '${url}/api/v2/default_catalog/${db[0]}/sql' -u 'root:' -d '{"query": "select 1;":"xxx"}' --header "Content-Type: application/json"
-- result:
0
{"exception":"malformed json [ {\"query\": \"select 1;\":\"xxx\"} ]"}
{"code":"1","message":"malformed json [ {\"query\": \"select 1;\":\"xxx\"} ]"}
-- !result
sync;
-- result:
@ -133,7 +133,7 @@ sync;
shell: curl -X POST '${url}/api/v2/default_catalog/${db[0]}/sql' -u 'root:' -d '{"sessionVariables":{"broadcast_row_limit":14000000}}' --header "Content-Type: application/json"
-- result:
0
{"exception":"\"query can not be empty\""}
{"code":"1","message":"\"query can not be empty\""}
-- !result
sync;
-- result:
@ -141,7 +141,7 @@ sync;
shell: curl -X POST '${url}/api/v2/default_catalog/${db[0]}/sql' -u 'root:' -d '{"query": "select 1;", "sessionVariables":{"broadcast":14000000}}' --header "Content-Type: application/json"
-- result:
0
{"exception":"Unknown system variable 'broadcast', the most similar variables are {'broadcast_row_limit', 'broadcast_right_table_scale_factor', 'disable_colocate_join'}"}
{"code":"1","message":"Unknown system variable 'broadcast', the most similar variables are {'broadcast_row_limit', 'broadcast_right_table_scale_factor', 'disable_colocate_join'}"}
-- !result
sync;
-- result:
@ -149,7 +149,7 @@ sync;
shell: curl -X POST '${url}/api/v2/default_catalog/${db[0]}/sql' -u 'root:' -d '{"query": "select from duplicate_table_with_null;", "disablePrintConnectionId":true}' --header "Content-Type: application/json"
-- result:
0
{"exception":"Getting syntax error at line 1, column 7. Detail message: Unexpected input 'from', the most similar input is {a legal identifier}."}
{"code":"1","message":"Getting syntax error at line 1, column 7. Detail message: Unexpected input 'from', the most similar input is {a legal identifier}."}
-- !result
sync;
-- result:
@ -157,18 +157,18 @@ sync;
shell: curl -X POST '${url}/api/v2/default_catalog/${db[0]}/sql' -u 'root:' -d '{"query": "select * from duplicate_table_with_null;select 1 from duplicate_table_with_null;", "disablePrintConnectionId":true}' --header "Content-Type: application/json"
-- result:
0
{"exception":"/api/v2/<catalog_name>/<database_name>/query does not support execute multiple query"}
{"code":"1","message":"/api/v2/<catalog_name>/<database_name>/query does not support execute multiple query"}
-- !result
shell: curl -X POST '${url}/api/v2/default_catalog/${db[0]}/sql' -u 'root:' -d '{"query": "set profile_timeout =60", "disablePrintConnectionId":true}' --header "Content-Type: application/json"
-- result:
0
{"exception":"/api/v2/<catalog_name>/<database_name>/query only support SELECT, SHOW, EXPLAIN, DESC, KILL statement"}
{"code":"1","message":"/api/v2/<catalog_name>/<database_name>/query only support SELECT, SHOW, EXPLAIN, DESC, KILL statement"}
-- !result
shell: curl -X POST '${url}/api/v2/default_catalog/${db[0]}/sql' -u 'root:' -d '{"query": "kill 314159265432222;", "disablePrintConnectionId":true}' --header "Content-Type: application/json"
-- result:
0
{"exception":"Unknown thread id: 314159265432222"}
{"code":"1","message":"Unknown thread id: 314159265432222"}
-- !result
sync;
-- result:
-- !result
-- !result