JSON exception add

This commit is contained in:
Igor I
2024-12-24 11:59:04 +05:00
parent 383c79fcd8
commit 00edb2c55c
4 changed files with 257 additions and 207 deletions

View File

@ -134,7 +134,7 @@
<version>2.22.2</version>
<configuration>
<systemPropertyVariables>
<spring.config.location>file:kz_mcp_jwt.properties</spring.config.location>
<spring.config.location>file:org_ccalm_jwt.properties</spring.config.location>
</systemPropertyVariables>
</configuration>
</plugin>

View File

@ -127,19 +127,27 @@ public class MainController implements ServletContextAware {
public String createStrJSONError(int code, String message, String setting, String marker) {
JSONObject json = new JSONObject();
try {
json.put("error_code", code);
json.put("error_message", Arrays.asList(message));
json.put("error_setting", Arrays.asList(setting));
json.put("error_marker", marker);
} catch (JSONException e) {
return "{}";
}
return json.toString();
}
public JSONObject createJSONError(int code, String message, String setting, String marker) {
JSONObject json = new JSONObject();
try {
json.put("error_code", code);
json.put("error_message", Arrays.asList(message));
json.put("error_setting", Arrays.asList(setting));
json.put("error_marker", Arrays.asList(setting));
} catch (JSONException e) {
logger.error(e);
}
return json;
}
@ -266,6 +274,7 @@ public class MainController implements ServletContextAware {
@ResponseBody
public String index(Model model,@RequestParam(required=false,name="lng",defaultValue = "1") String language_id) {
JSONObject json = new JSONObject();
try {
json.put("error_code",0);
json.put("error_message","");
json.put("error_marker",(String)null);
@ -287,6 +296,9 @@ public class MainController implements ServletContextAware {
json.put("name",application_name);
//json.put("active_connections",dataSource.getHikariPoolMXBean().getActiveConnections());
//json.put("idle_connections",dataSource.getHikariPoolMXBean().getIdleConnections());
} catch (JSONException e) {
throw new RuntimeException(e);
}
return json.toString();
}
/*
@ -299,10 +311,11 @@ public class MainController implements ServletContextAware {
public String get_settings(@CookieValue(value = "jwt_a", defaultValue = "") String jwt_a, @RequestParam(required=false,name="lng",defaultValue = "1") String language_id) {
Translation trt = new Translation(language_id,jdbcTemplate);
JSONObject json = new JSONObject();
try{
json.put("error_code",0);
//json.put("error_message","");
//json.put("error_marker",(String)null);
try{
if(jwt_a.equals("") || countOccurrences(jwt_a, '.')!=2)
{
throw new CustomException(10000, trt.trt("Please_send_a_valid_JWT_token"),null);
@ -355,10 +368,11 @@ public class MainController implements ServletContextAware {
public String set_settings(SettingModel setting, @CookieValue(value = "jwt_a", defaultValue = "") String jwt_a, @RequestParam(required=false,name="lng",defaultValue = "1") String language_id) {
Translation trt = new Translation(language_id,jdbcTemplate);
JSONObject json = new JSONObject();
try{
json.put("error_code",0);
//json.put("error_message","");
//json.put("error_marker",(String)null);
try{
if(jwt_a.equals("") || countOccurrences(jwt_a, '.')!=2)
{
throw new CustomException(10000, trt.trt("Please_send_a_valid_JWT_token"),null);
@ -423,8 +437,8 @@ public class MainController implements ServletContextAware {
public String access(Model model, @CookieValue(value = "jwt_a", defaultValue = "") String jwt_a,@Nullable @RequestBody ActionName action_name,@CookieValue(value = "lng",defaultValue = "1") String language_id) {
Translation trt = new Translation(language_id,jdbcTemplate);
String result=createStrJSONError(10000,trt.trt("Request_not_processed"), (String)null, (String)null);
try {
if(jwt_a.equals("") || countOccurrences(jwt_a, '.')!=2)
{
result=createStrJSONError(10000,trt.trt("Please_send_a_valid_JWT_token"), (String)null, (String)null);
@ -472,7 +486,11 @@ public class MainController implements ServletContextAware {
}
json.put("data",data);
result = json.toString();
} catch (Exception e) {
String uuid = UUID.randomUUID().toString();
logger.error(uuid,e);
result=createStrJSONError(10000,trt.trt("Internal_Server_Error"), (String)null, uuid);
}
return result;
}
@ -481,9 +499,10 @@ public class MainController implements ServletContextAware {
public String captcha(Model model, @RequestBody EmailModel email_model, @RequestParam(required=false,name="lng",defaultValue = "1") String language_id) {
Translation trt = new Translation(language_id,jdbcTemplate);
JSONObject json = new JSONObject();
try{
json.put("error_code",0);
json.put("error_message","");
try{
//Генерю Captcha
ImageCaptcha imageCaptcha = new ImageCaptcha.Builder(400, 100)
.addContent(new LatinContentProducer(7),
@ -532,9 +551,10 @@ public class MainController implements ServletContextAware {
public String create(@RequestBody NewUserModel newUserModel,@RequestParam(required=false,name="lng",defaultValue="1") String language_id) {
Translation trt = new Translation(language_id,jdbcTemplate);
JSONObject json = new JSONObject();
try{
json.put("error_code",0);
json.put("error_message","");
try{
if(newUserModel.getName().length()<3) {
throw new CustomException(10000, trt.trt("The_name_field_is_empty"),null);
}
@ -665,9 +685,10 @@ public class MainController implements ServletContextAware {
public String info(Model model, @CookieValue(value = "jwt_a", defaultValue = "") String jwt_a, @CookieValue(value = "lng",defaultValue="1") String language_id) {
Translation trt = new Translation(language_id,jdbcTemplate);
JSONObject json = new JSONObject();
try {
json.put("error_code",0);
json.put("error_message","");
try {
if(jwt_a.equals("") || countOccurrences(jwt_a, '.')!=2)
{
throw new CustomException(10000, trt.trt("Please_send_a_valid_JWT_token"),null);
@ -733,9 +754,10 @@ public class MainController implements ServletContextAware {
Translation trt = new Translation(language_id,jdbcTemplate);
JSONObject json = new JSONObject();
try {
json.put("error_code",0);
json.put("error_message","");
try {
if(loginModel.getLogin().isEmpty())
throw new CustomException(10000,trt.trt("The_login_field_is_empty"),null);
if(!Tools.isValidEmail(loginModel.getLogin()))
@ -1032,6 +1054,10 @@ public class MainController implements ServletContextAware {
java.lang.String uuid = UUID.randomUUID().toString();
logger.error("Error executing SQL query", uuid, ex);
throw new CustomException(10000, trt.trt("Error_executing_SQL_query"),uuid);
}catch (Exception e) {
String uuid = UUID.randomUUID().toString();
logger.error(uuid,e);
throw new CustomException(10000, trt.trt("Internal_Server_Error"),uuid);
}
Map<String, Integer> result = new HashMap<>();
@ -1047,9 +1073,10 @@ public class MainController implements ServletContextAware {
public String newtotp(HttpServletRequest request, @RequestBody LoginModel loginModel, @RequestParam(required=false,name="lng",defaultValue="1") String language_id) {
Translation trt = new Translation(language_id,jdbcTemplate);
JSONObject json = new JSONObject();
try {
json.put("error_code",0);
json.put("error_message","");
try {
if(loginModel.getLogin().isEmpty())
throw new CustomException(10000,trt.trt("The_login_field_is_empty"),null);
if(!Tools.isValidEmail(loginModel.getLogin()))
@ -1219,9 +1246,9 @@ public class MainController implements ServletContextAware {
Translation trt = new Translation(language_id,jdbcTemplate);
JSONObject json = new JSONObject();
try {
json.put("error_code",0);
json.put("error_message","");
try {
if(jwt_a.equals("") || countOccurrences(jwt_a, '.')!=2 || jwt_r.equals("") || countOccurrences(jwt_r, '.')!=2 )
{
@ -1317,7 +1344,7 @@ public class MainController implements ServletContextAware {
Translation trt = new Translation(language_id,jdbcTemplate);
String result=createHTMLError(1,trt.trt("Request_not_processed"));
try {
int index = token.indexOf(".");
if (index < 0)
return createHTMLError(10000, trt.trt("Please_send_a_valid_token"));
@ -1326,8 +1353,7 @@ public class MainController implements ServletContextAware {
String signature1 = token.substring(index + 1);
String signature2 = Tools.generateSignature(captchaKey, payload);
if(! signature1.equals(signature2))
{
if (!signature1.equals(signature2)) {
return createHTMLError(1, trt.trt("The_signature_did_not_match"));
}
@ -1360,7 +1386,11 @@ public class MainController implements ServletContextAware {
parameters = new MapSqlParameterSource();
parameters.addValue("email", jToken.getString("email"));
int cnt = jdbcTemplate.update(sql, parameters);
} catch (Exception e) {
String uuid = UUID.randomUUID().toString();
logger.error(uuid,e);
return createHTMLError(10000,trt.trt("Internal_Server_Error"));
}
return createHTMLError(0,trt.trt("The_password_has_been_changed_and_you_will_be_redirected_to_the_main_page"));
}
@ -1369,8 +1399,8 @@ public class MainController implements ServletContextAware {
public String restore(Model model, @RequestBody RestoreModel restore, @RequestParam(required=false,name="lng",defaultValue = "1") String language_id) {
Translation trt = new Translation(language_id,jdbcTemplate);
String result=createStrJSONError(10000,trt.trt("Request_not_processed"), (String)null, (String)null);
try{
//Connection conn = getConnection();
int index = restore.getToken().indexOf(".");
@ -1455,6 +1485,11 @@ public class MainController implements ServletContextAware {
logger.error(uuid, ex);
return createStrJSONError(10000,trt.trt("Failed_send_mail_to_s"), token.getString("email"),uuid);
}
} catch (Exception e) {
String uuid = UUID.randomUUID().toString();
logger.error(uuid,e);
return createStrJSONError(10000,trt.trt("Internal_Server_Error"), (String)null,uuid);
}
return createStrJSONError(0, trt.trt("A_recovery_link_has_been_sent_to_your_email"),(String)null,(String)null);
}
@ -1464,9 +1499,10 @@ public class MainController implements ServletContextAware {
Translation trt = new Translation(language_id,jdbcTemplate);
JSONObject json = new JSONObject();
try {
json.put("error_code",0);
json.put("error_message","");
try {
if(update==null)
throw new CustomException(10000,trt.trt("Please_send_a_valid_JSON_string_in_your_request"),null);
if(update.getLogin().equals(""))
@ -1547,6 +1583,10 @@ public class MainController implements ServletContextAware {
} catch (CustomException e) {
json = e.getJson();
} catch (Exception e) {
String uuid = UUID.randomUUID().toString();
logger.error(uuid,e);
return createStrJSONError(10000,trt.trt("Internal_Server_Error"), (String)null,uuid);
} finally {
}
return json.toString();

View File

@ -1,11 +1,15 @@
package org.ccalm.jwt.tools;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Arrays;
import java.util.List;
public class CustomException extends Exception {
private static final Logger logger = LogManager.getLogger(CustomException.class);
private int errorCode;
private String marker;
private List<String> errorMessages;
@ -59,10 +63,14 @@ public class CustomException extends Exception {
public JSONObject getJson() {
JSONObject json = new JSONObject();
try {
json.put("error_code", this.getErrorCode());
json.put("error_message", this.getErrorMessages());
json.put("error_setting", this.getErrorSettings());
json.put("error_marker", this.getErrorMarker());
} catch (JSONException e) {
logger.error("Error", e);
}
return json;
}
}

View File

@ -80,6 +80,8 @@ public class Storage implements AutoCloseable {
}
} catch (SQLException e) {
logger.error("An error occurred", e);
} catch (Exception e) {
logger.error("An error occurred", e);
}
return result;
}