Last time said the log, do not know the old iron met no, the log printed a lot of, really to find a cause of abnormal and error. What is the root cause of this problem? Because the system does not have a uniform exception specification. This can be dangerous when an exception is detected and the e.printStackTrace() simply prints out the stack. If there is no unified processing of exceptions in the early stage, it is really very difficult to unify and adjust them in the later stage, and the coupling between exceptions and our business logic is very deep. It’s very, very hard to reconcile. So at the beginning of the design system must be designed to improve. If the system and service exceptions are not standardized at the beginning, it is difficult to monitor them even in the later distributed stage. The monitoring system cannot know whether they are system exceptions or service exceptions. Unable to accurately inform developers and operations personnel. Alarm do not know how to alarm, for example: a password output error may trigger the alarm, what is the result? The alarm volume is very, very large. If we don’t call the police, when something does go wrong, it’s over. Is it between a rock and a hard place? No, no, no.

Scenario review

  • Have you ever met old iron?

Is that a classic summary? It’s not even my pot. We’re all grown men.

What should we do if there is an anomaly

In fact, reflects the problem, if the user has a problem, as long as the prompt is clear, the user will not reflect the phone customer service there. If users are also required to give feedback, it means that the abnormal design of the system is unreasonable. Our monitoring system has not done so well. Users know this problem, but our system does not know it. What the monitoring system needs to do is to know before the user gives feedback. If the exception definition is good and strong, you’ll know where the problem is! Many systems now take a long time to locate a problem. So we can figure out what went wrong. Now let’s figure out where the problem happened, achieve a goal, quickly respond, quickly know the corresponding problem, locate the root cause of the problem, the other party is wrong, there are clear instructions. It should not be brought online, it should be brought into production, it should be killed before it goes online.

The starting point of system exception design

  1. Good abnormal information prompt, development operation and maintenance personnel can quickly locate
  2. When responding to an external call exception, it should be clear whether it is an internal exception or if the call condition is not met.
  3. In response to user operation exceptions, it can kindly prompt users.

Abnormal classification

Internal abnormal

The response cannot be returned as expected by the user.

  • Resource environment (system environment exception, database connection timeout, third-party service response timeout)
  • The third-party service responded incorrectly

It has been transferred to the third-party system. The third-party system itself has a bug, which is caused by the bug

  • The response result of the third party is incorrect

Returns 1 and 0 by convention, and returns -1.

  • The external parameter is invalid

Someone else calls their system and explicitly tells it that the argument was passed incorrectly.

  • Bad encoding logic

Call argument, pass 1-10, you pass 11.

  • Wrong configuration

The live code links to the test database

  • Abnormal business data (business data missing)

Error code passing, custId and userId are written backwards.

Business exceptions

The fault is caused by incorrect user operations, for example, incorrect password.

  • User error

Catch exceptions.

  • Service conditions are not met

Business time specification in advance.

The system correctly catches this type of exception and throws ####1

  • For interfaces provided outside of the system (validation immediately after invocation, not after a logical step), parameter validation is required (must)
  • The system provides internal and external interfaces for authentication
  • Tool class for parameter verification
  • The public method is validated
  • Private method (parameter validation is not recommended)

2. Verify the validity of third-party response results

  • After obtaining the third method results, verify according to your agreement

3. Verify service prerequisites before processing services

  • Before business processing, verify business conditions (verify amount, verify whether the account is locked by the public security)
  • Consider performance cost (verify id number exists)

4. Verify the result after the service is processed

  • Verify the other side account arrived account, turn an account to deduct money successfully

5. Try catch the code that may be abnormal

  • Attempt recovery processing
  • Direct selling
  • Cast after conversion

### System exit unified interception processing

The purpose of unified interception is to ensure that the outgoing exception is controllable and the caller can understand the information of the exception. Here, the exit refers to the unified response logic of the system. Generally, there are three scenarios.

####1. Web Response

H5, PC page

  • Internal abnormal

The exception page is displayed

  • Business exceptions

Returns the corresponding prompt message to the front end

  • Unknown abnormal

Try to identify, if not, convert to exception code

####2. Http API response

  • Internal abnormal

Return interface unavailable message

  • Parameter error

The response is returned based on the exception list in the API documentation. The parameter is invalid, and you need to invoke the method to verify the parameter validity

  • Business errors

Returns the corresponding code and message based on non-convention

####3. RPC Service interface response

  • Internal abnormal

Return service unavailable message

  • Parameter error

Responds based on the interface document, returning the exception stack directly

  • Business errors

Return the exception stack directly

CheckedException and uncheckedException declare principles

    1. If the argument is thrown illegally, the result returned is illegal (that is, a software BUG) uncheckedException
    1. Throw checked exceptions if you think the calling programmer needs to take conscious action.
    1. Application products have explicit conditional requirements to declare detected business exceptions

Uniformly classify exceptions

  • Abnormal conversion
  • Exception Information processing
  • Logic assertions
  • Parameter validity Verification
  • Verify the validity of the result

Exception handling

Intercept and handle exceptions in a unified manner

  • Objective: To prevent undefined abnormal outflow from the system
  • RPC Service response interception
  • Web Control response interception
  • Http API response interception

Common error handling of exceptions

  • Direct not slightly abnormal
try { 
new String(source.getBytes("UTF-8"), "GBK"); 
} catch (UnsupportedEncodingException e) { 
e.printStackTrace(); 
} 

Copy the code
  • Correct handling
try { 
new String(source.getBytes("UTF-8"), "GBK"); 
} catch (UnsupportedEncodingException e) { 
throw new RuntimeException("The environment does not support UTF-8",e) 
} 
Copy the code
  • The business does not provide any information
public class DuplicateUsernameException extends Exception {}Copy the code
  • Define a Code for each exception handling and replace all business exceptions with a uniform exception
public class ServiceException extends RuntimeException { 

public ServiceException(Exception e) { 
  super(e); 
} 

public ServiceException(String message) { 
  super(message); }}Copy the code

Error:

1. The service exception must be clearly defined. 2

PS: It is particularly important to judge the abnormality of the robust system. Do not think that the development is completed. In fact, in the development process, just like decoration, the “front door” is very bright, and the “back door” should be well controlled. What if someone doesn’t come through the front door, asks for a key, and comes in the back door with an axe?