Home > Back-end >  After an exception is thrown, also can return to hint to the front desk?
After an exception is thrown, also can return to hint to the front desk?

Time:11-25

 
@ Transactional (rollbackFor=Exception class, propagation=propagation. The REQUIRED)
Public Map SaveNewOrder Order (Order) {
Map The map=new HashMap<> (a);

//save order
Try {
OrderMapper. InsertOrder (order);
} the catch (Exception e) {
Throw new RuntimeException (" 6546666666666666666666666 ");
}

//save log
Try {
OrderLogMapper. InsertLog (orderLog);
} the catch (Exception e) {
throw new RuntimeException(e);
The map. The put (" code ", "1002");
Map. The put (" MSG ", "create order log exceptions");
return map;
}

Map Data=https://bbs.csdn.net/topics/new HashMap <> ();
The map. The put (" code ", "0");
return map;
}


As shown in the code, assume save log failure, throw new RuntimeException (e); After the rollback is successful, but the back of the code will not be carried out,
Question: after an exception is thrown, tip can also return a custom content to the front desk? Avoid error code back to the server to the front desk,

Thank you ~

CodePudding user response:

Need to escape the error message and translate technical error into business error information

CodePudding user response:

Usually in the outermost handle exceptions can
If for springmvc, find a bean inheritance HandlerExceptionResolver, then encapsulated exception information, if it is other, can consider to make a filter in the outermost try catch exceptions, then wrap the exception message, if it is a custom exception, the abnormal response message, if it is other abnormal directly to 500 page

A small suggestion, the method return type is the best type of business significance, not has nothing to do with the business of news, the prompt message transmitted by abnormal, such code simple and clear, and the try catch exceptions remember print log

CodePudding user response:

Who throws an exception, with clients who accept treatment, who call who deal with the principle of the method you SpingMVC internal call, will be handled by the MVC, runtimeException should terminate this operation, the front desk can accept is 500

CodePudding user response:

reference 1st floor tianfang response:
need to escape the error message and translate technical error into business error message


The right is such,
To the front desk to deal with prompt to the customer

CodePudding user response:

Increase the finally {, put behind must perform} in it

CodePudding user response:

666, thank you
  • Related