Memo on pushing abnormal messages of nailing robot

Posted by php-coder on Sun, 16 Jan 2022 09:12:47 +0100

The developed quick selling link connects with Qimen's user-defined interface and data security out of the tower project. Because the project needs to be deployed to the stone gathering tower, but it is troublesome to view the abnormal information, which is not conducive to troubleshooting the error information, so it is considered to use the nailed robot to push the abnormal message.

First, you need a nailing account. Second, you need to pull a group

For example, I choose to create a project group

After creation, a message will be pushed. This is the nailing robot. Click its avatar to select more robots

 

I chose custom

 

Select the ip address segment. The setting here is equivalent to the ip white list. Only the ip in the address segment is allowed to push messages

After configuration, a link address will be given. Please copy this address for storage. This is the address where you push messages

 

The following link is the download address of nailing Open Platform sdk. Using sdk for development is relatively fast and convenient. There are development cases on the document

https://developers.dingtalk.com/document/resourcedownload/download-server-sdk?pnamespace=app

The following is the development document of user-defined robot access. I developed it on this basis. It supports message push in text, link and markdown formats

 https://developers.dingtalk.com/document/app/custom-robot-access

Next, I introduce the use in the project. The project I created is the springboot project. The idea is to establish a global exception capture mechanism to obtain the exception information and push the exception message of the nailing robot

package com.text..control;

import com.text.utils.DingTalkUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;

/**
 * @description: Global exception handling
 * @author: 
 * @date: 2021-07-15 8:48
 * @version: 1.0
 */
@ControllerAdvice
public class ExceptionHandler {


    @org.springframework.web.bind.annotation.ExceptionHandler(value=Exception.class)
    public void exceptionHandler(Exception e){
        //Catch exceptions globally and push nailing exception messages
        DingTalkUtils.dingTalkSendException(e);
    }
}

Please remember to replace the link address in {DingTalkClient client = new DefaultDingTalkClient(), which is the robot push address copied above

    public static void dingTalkSendException(Exception e){
        //Get the first element of the exception stack
        StackTraceElement  stackTrace = e.getStackTrace()[0];
        StringBuilder sb=new StringBuilder();
        sb.append("Error message:"+e.getMessage()+"\n")
          .append("Error report file:"+stackTrace.getFileName()+"\n")
          .append("Wrong line number:"+stackTrace.getLineNumber()+"\n")
          .append("Error reporting method:"+stackTrace.getMethodName()+"\n")
          .append("report errors Class:"+stackTrace.getClass()+"\n")
          .append("report errors ClassName:"+stackTrace.getClassName());
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send");
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMsgtype("text");
        OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
        text.setContent(sb.toString());
        request.setText(text);
        OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();

        at.setIsAtAll(true);
        request.setAt(at);


        OapiRobotSendResponse response=null;
        try {
            response= client.execute(request);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        System.out.println("Return information of nailing robot exception notification:"+response.getBody());
    }

The following is the handling of exception information in the express custom api interface

For example, in the following code, I call an interface of express, and its interface api has a structure indicating the return of exception information

        AliexpressCategoryRedefiningGetchildattributesresultbypostcateidandpathResponse rsp = client.execute(req, session);
        JSONObject bodyJson = JSONObject.parseObject(rsp.getBody());
        //The structure returned by express needs to be re integrated, and another layer needs to be removed outside the result
        JSONObject aliexpressResponse = bodyJson.getJSONObject("aliexpress_category_redefining_getchildattributesresultbypostcateidandpath_response");
        System.out.println(bodyJson);
        if(bodyJson.containsKey("error_response")){
            throw new RuntimeException(bodyJson.toString());
        }
        return  aliexpressResponse;
{
	"error_response":{
		"msg":"Remote service error",
		"code":50,
		"sub_msg":"illegal parameter",
		"sub_code":"isv.invalid-parameter"
	}
}

Therefore, judge whether the returned structure contains error_ The response key throws an exception if any

This is the rendering. In this way, you don't need to check the log of Qimen to locate the problem. Other abnormal information can be thrown normally

Topics: Java Maven IDE websocket