Code processing method and related device

By identifying anomalies during code generation and inserting exception handling and logging code, the problem of difficult exception identification in incremental development is solved, enabling earlier exposure and accurate location, reducing maintenance costs, and improving code stability and maintainability.

CN121523690APending Publication Date: 2026-02-13永赢金融租赁有限公司
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202511752784.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-11-26
Publication Date
2026-02-13

AI Technical Summary

Technical Problem

In incremental development based on code generation models, exceptions in newly added code are difficult to identify in a timely and comprehensive manner, and some exceptions are only exposed during the runtime phase, which increases the difficulty of troubleshooting and maintenance.

Method used

By parsing the requirement description information corresponding to the current code and the newly added target code, the location and type of exception points are identified, and exception handling code and logging code are inserted in the draft code stage to generate enhanced code.

Benefits of technology

It reduces the occurrence of missing exceptions and logs, enabling exceptions to be exposed and accurately located earlier, shortening troubleshooting time, reducing maintenance costs, and improving code stability and maintainability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121523690A_ABST
    Figure CN121523690A_ABST
Patent Text Reader

Abstract

The invention discloses a code processing method and a related device, and the method comprises the steps: analyzing the demand description information of a current code corresponding to a newly-added target code, recognizing the position where an exception possibly occurs before the code is really generated, and inserting an exception processing code and a log processing code in a draft code stage. According to the method, the newly added code has the capability of performing exception processing and log processing at a specific position after being combined with the existing code, the conditions of exception omission and log missing are reduced, and meanwhile, the exception condition can be exposed earlier and accurately positioned, so that the troubleshooting time is shortened, the maintenance cost is reduced, and the working efficiency is improved. And the overall stability and maintainability of the code are improved.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of computer technology, and in particular, to a code processing method and related apparatus. BACKGROUND

[0002] With the rapid development of artificial intelligence, automatic code generation tools based on code generation models are widely used in the field of software development. Developers can input requirement description information, and the code generation model can automatically generate code that meets the functional requirements, thereby improving the efficiency of code writing.

[0003] In related technologies, exception handling and log processing are usually two independent links in the code development process, and are manually supplemented by developers after the code is written. For example, before the code is run, the developer adds an exception capture statement at a specific location and calls a log interface in the exception capture statement to output exception information.

[0004] However, in the incremental development process based on the code generation model, such as continuously obtaining new code through a prompt word dialogue, the exceptions in the newly added code are difficult to be fully identified in time, and some exceptions are only exposed in the running stage, which increases the difficulty of problem troubleshooting and maintenance. SUMMARY

[0005] To solve the above problems, the present application provides a code processing method and related apparatus to reduce the situation of exception omission and log loss, so that the exception positioning is more timely.

[0006] Based on this, the present application discloses the following technical solutions:

[0007] In a first aspect, the present application provides a code processing method, which comprises:

[0008] obtaining target code newly added to current code and requirement description information corresponding to the target code, wherein the target code is code generated by a code generation model based on the requirement description information;

[0009] parsing the requirement description information corresponding to the current code and the target code to obtain structure information of the current code and a functional intent corresponding to the requirement description information;

[0010] determining an exception point position and an exception point type according to the structure information and the functional intent;

[0011] Before generating a draft code, inserting exception handling code and log processing code into the draft code according to the exception point position and the exception point type to obtain enhanced code, wherein the draft code comprises the current code and the target code.

[0012] Optionally, the log processing code includes trace identifier generation code, which generates a trace identifier to uniquely identify the request of the enhancement code at the anomaly point location.

[0013] Optionally, based on the location and type of the anomaly, the step of inserting exception handling code and log handling code into the draft code to obtain enhanced code includes:

[0014] Based on the anomaly type, a target anomaly handling template corresponding to the anomaly type is determined. The anomaly handling template is used to indicate the log level, log content placeholder, insertion method of tracking identifier, and anomaly recovery strategy. The target anomaly handling template is one of multiple anomaly handling templates.

[0015] Based on the target exception handling template, generate the exception handling code and the log handling code;

[0016] The exception handling code and the log handling code are inserted into the draft code to obtain the enhanced code.

[0017] Optionally, the method further includes:

[0018] Retrieve historical project code;

[0019] Identify multiple anomaly point type samples, anomaly handling code samples corresponding to the multiple anomaly point types, and log processing code samples corresponding to the multiple anomaly point type samples in the historical project code.

[0020] Statistical analysis is performed on multiple anomaly type samples, anomaly handling code samples corresponding to multiple anomaly type samples, and log processing code samples corresponding to multiple anomaly type samples to obtain high-frequency anomaly handling codes and high-frequency log processing codes corresponding to the multiple anomaly type samples.

[0021] The exception handling template is generated based on the high-frequency exception handling code and the high-frequency log handling code.

[0022] Optionally, the method further includes:

[0023] Obtain the runtime environment of the current code;

[0024] If the operating environment is a development environment, then the log transmission strategy included in the log processing code is to output log information through the local console;

[0025] If the running environment is a test environment, the log processing code includes a log transmission strategy of writing the log information into a local log document and uploading the log information to a log server in batches through an asynchronous task;

[0026] If the running environment is a production environment, the log processing code includes a log transmission strategy of uploading the log information to the log server in real time.

[0027] Optionally, if the log processing code is used to generate a tracking identifier, after the enhanced code is generated, the method further includes:

[0028] verifying the enhanced code to obtain a verification result of the enhanced code;

[0029] If the verification result indicates that the enhanced code is missing or non-compliant, the update exception handling code or the update log processing code is generated through a pre-trained code reconstruction model and is merged into the enhanced code;

[0030] The verification includes at least one of the following verification manners:

[0031] detecting whether the abnormal point position is inserted into corresponding exception handling code and log processing code;

[0032] detecting whether the tracking identifier generated by the log processing code is unique;

[0033] verifying whether the log processing code covers corresponding context variables.

[0034] In a second aspect, an embodiment of the present application provides a code processing apparatus, and the apparatus includes:

[0035] an obtaining unit configured to obtain target code added to current code and requirement description information corresponding to the target code, wherein the target code is code generated based on the requirement description information through a code generation model;

[0036] a parsing unit configured to parse the requirement description information corresponding to the current code and the target code to obtain structure information of the current code and a functional intention corresponding to the requirement description information;

[0037] a determining unit configured to determine an abnormal point position and an abnormal point type according to the structure information and the functional intention;

[0038] an inserting unit configured to insert, before generating a draft code, exception handling code and log processing code into the draft code according to the abnormal point position and the abnormal point type to obtain enhanced code, wherein the draft code includes the current code and the target code.

[0039] In a third aspect, an embodiment of the present application provides a computer device, comprising a processor and a memory:

[0040] The memory is configured to store a computer program and transmit the computer program to the processor.

[0041] The processor is configured to execute the method of the first aspect above according to the computer program.

[0042] In a fourth aspect, an embodiment of the present application provides a computer readable storage medium configured to store a computer program, the computer program being configured to execute the method of the first aspect above.

[0043] In a fifth aspect, an embodiment of the present application provides a computer program product comprising a computer program, which, when executed on a computer device, causes the computer device to execute the method of the first aspect above.

[0044] From the above technical solutions, the present application has at least the following beneficial effects:

[0045] The target code newly added for the current code and the requirement description information corresponding to the target code are obtained, wherein the target code is code generated based on the requirement description information through a code generation model. The requirement description information corresponding to the current code and the target code is parsed to obtain structure information of the current code and a functional intent corresponding to the requirement description information. The abnormal point position and the abnormal point type are determined according to the structure information and the functional intent. Before generating the draft code, the abnormal handling code and the log handling code are inserted into the draft code according to the abnormal point position and the abnormal point type to obtain the enhanced code, and the draft code includes the current code and the target code. Therefore, by parsing the requirement description information corresponding to the current code and the newly added target code, the position where the abnormality may occur is identified before the code is actually generated, and the abnormal handling code and the log handling code are inserted in the draft code stage, so that the newly added code has the ability to perform abnormal handling and log handling at a specific position after being merged into the existing code, reducing the situation of abnormal omission and log loss, and making the abnormal situation be exposed and located accurately earlier, which is beneficial to shorten the problem troubleshooting time, reduce the maintenance cost, and improve the overall stability and maintainability of the code. BRIEF DESCRIPTION OF DRAWINGS

[0046] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the accompanying drawings needed to be used in the description of the embodiments or the prior art will be briefly introduced. Obviously, the accompanying drawings in the following description only represent some embodiments described in the present application, and for those skilled in the art, other drawings can also be obtained without creative labor on the basis of these drawings.

[0047] Figure 1 A flowchart of a code processing method provided by an embodiment of the present application;

[0048] Figure 2 A structural diagram of a code processing device provided by an embodiment of the present application;

[0049] Figure 3 A structural diagram of a computer device provided by an embodiment of the present application. DETAILED DESCRIPTION

[0050] The embodiments of the present application will be described in more detail below with reference to the accompanying drawings. Although some embodiments of the present application are shown in the drawings, it should be understood that the present application can be implemented in various forms, and should not be interpreted as being limited to the embodiments set forth herein, but rather, these embodiments are provided to more thoroughly and completely understand the present application. It should be understood that the drawings and embodiments of the present application are only for exemplary purposes, and are not intended to limit the scope of protection of the present application.

[0051] In the incremental development process based on the code generation model, in the related art, two ways are usually used to write exception handling related code, the developer writes code after multiple rounds of code addition, or the developer writes code after each round of code addition, which is low in efficiency, and sometimes logs are missed, making it difficult to troubleshoot abnormal problems.

[0052] Based on this, the embodiments of the present application provide a code processing method and related device, by analyzing the requirement description information corresponding to the current code and the newly added target code, the position where the exception may occur is identified before the code is actually generated, and the exception handling code and the log processing code are inserted in the draft code stage, so that the newly added code has the ability to perform exception handling and log processing at a specific position after being merged into the existing code, reducing the situation of exception omission and log loss, and also making the abnormal situation be exposed and accurately located earlier, which is beneficial to shorten the problem troubleshooting time, reduce the maintenance cost, and improve the overall stability and maintainability of the code.

[0053] The code processing method provided in the application can be applied to a computer device with code processing capability, such as a terminal device and a server. The terminal device can be a desktop computer, a notebook computer, a mobile phone, a tablet computer, or the like; and the server can be a physical server or a server cluster or a distributed system formed by multiple physical servers. The terminal device and the server can be directly or indirectly connected through wired or wireless communication, which is not limited in the application.

[0054] All data (such as current code and target code) collected in the application is collected with the consent and authorization of the object (such as a user, an institution or an enterprise) to which the data belongs, and the collection, use and processing of related data need to comply with relevant laws, regulations and standards of the country and region.

[0055] Referring to Figure 1 The figure is a flowchart of the code processing method provided in the embodiment of the application. In order to facilitate description, the following embodiment takes the server as an example to introduce the execution subject of the code processing method. As Figure 1 The code processing method includes S101-S104.

[0056] S101: Obtain target code newly added for current code and requirement description information corresponding to the target code.

[0057] The current code is an existing code, which is used as a carrier object of subsequent newly added code.

[0058] The target code is newly added code, which is generated by a code generation model based on requirement description information. The requirement description information is text information or instruction information used to describe the functional requirements of the target code. The requirement description information is usually input by a developer in natural language form, which is used to guide the code generation model to generate corresponding code.

[0059] The code generation model is an artificial intelligence model capable of automatically generating code. For example, the developer inputs "add order deletion function" to the code generation model, and the code generation model can automatically generate the corresponding deletion function code.

[0060] It should be noted that the execution time of S101 is after the newly added target code is generated, and before the next newly added code is generated. For example, in the process of multiple rounds of dialogue with the code generation model, the execution time of S101 is before the next round of dialogue.

[0061] S102: Analyze the requirement description information corresponding to the current code and the target code to obtain the structure information of the current code and the functional intent corresponding to the requirement description information.

[0062] By parsing the current code, the structural information of the current code can be obtained, and by parsing the requirement description information corresponding to the target code, the functional intent corresponding to the requirement description information can be obtained.

[0063] The structural information of the current code is used to describe the organization mode of the code, and can include function structure, class structure, call relationship, control flow, variable dependency relationship, and corresponding positions respectively.

[0064] The functional intent is the target function or business target reflected by the requirement description information. For example, the requirement description information is "add user query interface", and the functional intent is "implement query function".

[0065] The embodiment of the application does not specifically limit how to parse the current code. For example, the function structure, class structure, control flow and variable dependency relationship in the current code can be identified by performing syntax analysis on the current code. For another example, the call relationship, exception capture position and key statement in the code can also be identified by constructing an abstract syntax tree for the current code. For another example, the logic branch, loop structure and resource access statement in the code can also be analyzed by performing static code scanning on the current code to obtain the structural information of the current code.

[0066] The embodiment of the application does not specifically limit how to parse the requirement description information. For example, the keywords and operation objects of the target function in the requirement description information can be extracted by performing semantic analysis on the requirement description information. For another example, the business meaning and functional purpose corresponding to the requirement description information can be identified by performing natural language processing on the requirement description information. For another example, the functional intent expressed by the requirement description information can also be determined by performing text feature matching on the requirement description information, including the type of new function, operation behavior and expected processing result.

[0067] S103: Determine the exception point position and the exception point type according to the structural information and the functional intent.

[0068] The exception point position is the specific code position where the abnormal behavior may occur during code execution. The exception point type is the classification result of the abnormal scenario.

[0069] In the case of determining the functional intent, the specific position where the exception may occur can be determined through the structural information, so as to determine the exception point position, and the exception point type is determined according to the code type corresponding to the exception point position.

[0070] For example, the exception point position and the exception point type can be represented in the form of a list of potential exception points, as follows: [

[0072] {"type": "database_query", "location": "line 12", "risk": "connection_error"},

[0073] {"type": "network_request", "location": "line 35", "risk": "timeout"} ]

[0075] Wherein, type is the function intention, location is the abnormal point position, and risk is the abnormal point type.

[0076] S104: Before generating the draft code, according to the abnormal point position and the abnormal point type, insert the exception handling code and the log processing code in the draft code to obtain the enhanced code.

[0077] Wherein, the draft code includes the current code and the target code, and the draft code can be obtained by merging the target code and the current code.

[0078] It should be noted that before generating the draft code, it can be before outputting the draft code to the screen of the computer device, or before truly generating the draft code in the server background.

[0079] The exception handling code is a code logic for capturing and processing abnormal situations during program execution. For example, the exception handling code appears in the form of try-catch, try-except or other exception capturing structures, which is used to perform specific response operations when an exception occurs, such as recording error information, performing rollback operation or returning prompt, etc.

[0080] The log processing code is a code logic for recording information related to abnormal behavior during program execution. For example, the log processing code calls a log interface to write information such as exception type, exception location, context parameter, trace identifier, etc. into a log server, which is used for subsequent problem troubleshooting and running state analysis.

[0081] The enhanced code is the code formed after the insertion of the exception handling code and the log processing code based on the draft code.

[0082] The present application does not specifically limit how to generate the exception handling code and the log processing code, which will be illustrated by two ways as follows.

[0083] In a first mode, the draft code is positioned and parsed according to the position of the abnormal point to obtain the code statement where the abnormal point is located, and the code statement is marked as a to-be-processed node. Then, according to the type of the abnormal point, a corresponding exception handling rule is matched to generate an exception capture structure adapted to the type of the abnormal point, and the exception capture structure is inserted into an outer layer of the to-be-processed node, so that the code at the abnormal point position can be effectively captured in the execution process.

[0084] According to the type of the abnormal point, the context information of the current code is extracted, and the context information includes the position of the abnormal occurrence, the name of the related function, the key parameters, and the call path information. The context information and the abnormal information are combined to generate a log output statement, thereby forming a log processing code, and the log processing code is inserted into the inside of the exception capture structure, so that the corresponding log information can be recorded synchronously when the abnormality occurs.

[0085] In a second mode, an intermediate code representation structure is constructed for the draft code, for example, an abstract syntax tree or a control flow structure. By traversing the intermediate code representation structure, the node corresponding to the abnormal point is determined, and the generated exception handling code and log processing code are inserted at the node, thereby completing the conversion of the draft code to the enhanced code.

[0086] As can be seen from the above technical solutions, the target code newly added to the current code and the requirement description information corresponding to the target code are obtained, wherein the target code is generated based on the requirement description information by using a code generation model. The requirement description information corresponding to the current code and the target code is parsed to obtain the structure information of the current code and the functional intent corresponding to the requirement description information. According to the structure information and the functional intent, the position of the abnormal point and the type of the abnormal point are determined. Before the draft code is generated, the exception handling code and the log processing code are inserted into the draft code according to the position of the abnormal point and the type of the abnormal point to obtain the enhanced code, and the draft code includes the current code and the target code. Thus, by parsing the requirement description information corresponding to the current code and the newly added target code, the position where the abnormality may occur is identified before the code is actually generated, and the exception handling code and the log processing code are inserted in the draft code stage, so that the newly added code has the ability to perform exception handling and log processing at a specific position after being integrated into the existing code, reducing the situation of missing exception and missing log, and making the abnormal situation be exposed and located accurately earlier, which is beneficial to shorten the problem troubleshooting time, reduce the maintenance cost, and improve the overall stability and maintainability of the code.

[0087] In a possible implementation, the log processing code includes trace identification generation code, and the trace identification generation code is used to generate a trace identification (traceId) for uniquely identifying a request of the enhanced code at the abnormal point position.

[0088] In other words, the logging code not only simply records exception information, but also includes additional code to generate trace identifiers. These trace identifiers are used to identify a specific execution request of the enhanced code at the location of the exception.

[0089] In other words, when the program reaches an exception point, a unique trace identifier is generated by the trace identifier generation code, and this trace identifier is recorded along with the logs. Therefore, when the same code triggers an exception multiple times at different times or under different requests, each exception can be distinguished by its corresponding trace identifier, thus avoiding the difficulty of determining the source of multiple exception log entries being mixed together.

[0090] Therefore, by introducing tracking identifiers to avoid the confusion between logs triggered by different requests, the accuracy and traceability of anomaly localization are improved. This helps developers quickly determine the specific execution scenario in which the anomaly occurred, reduces the difficulty of troubleshooting caused by multiple anomalies intersecting, and improves the efficiency of problem analysis and the reliability of system maintenance.

[0091] In one possible implementation, a target exception handling template corresponding to the exception type can be determined based on the exception type. Exception handling code and logging code are then generated based on the target exception handling template. The exception handling code and logging code are then inserted into the draft code to obtain the enhanced code.

[0092] Among them, the exception handling template is a code template for exception handling code and logging code, used to indicate the log level, log content placeholders, the insertion method of tracking identifiers and exception recovery strategy. The target exception handling template is one of multiple exception handling templates corresponding to the exception point type.

[0093] Log level is a rating system used to indicate the importance of log information. Log content placeholders are reserved locations within the log information for filling in specific information; they are used to write actual exception data or context information into the log content during code execution. Trace flag insertion method describes the generation location and writing method of trace flags in the logging code, indicating how trace flags are associated with exception logs; for example, they can be inserted as independent fields or concatenated as part of the log information. Exception recovery strategy is the control method for program execution after an exception is caught, indicating the handling actions to be taken after an exception occurs.

[0094] Different anomaly types can correspond to different anomaly handling templates; that is, there can be a correspondence between anomaly handling templates and anomaly types. Therefore, based on this correspondence, the target anomaly handling template corresponding to the anomaly type can be determined.

[0095] For example, the configuration during the matching of exception handling templates is as follows:

[0096] {

[0097] "log_level": "error",

[0098] "log_content": "Database query failed",

[0099] "context_fields": ["query", "params"],

[0100] "report_method": "async_upload"

[0101] }

[0102] Here, log_level is the log level, log_content is the log content, context_fields is the context field configuration, and report_method is the log reporting method configuration.

[0103] After matching the target exception handling template, an example of exception handling code and logging code is as follows:

[0104] try {

[0105] const result = await db.query(sql, params);

[0106] } catch (err) { / / Exception handling code

[0107] const traceId = generateTraceId(); / / Statement to generate trace identifier

[0108] logger.error({

[0109] traceId,

[0110] message: "Database query failed",

[0111] params,

[0112] Error: err.stack

[0113] });

[0114] reportError(traceId, err); / / Log processing code

[0115] throw err;

[0116] }

[0117] As one implementation method, the reportError() function can be inserted to specify that the log is uploaded to a log server and support retries on failure.

[0118] Therefore, a corresponding target exception handling template is determined based on the type of anomaly, and exception handling code and log handling code are generated based on the target exception handling template. This allows different anomalies to be enhanced according to processing rules that match their type, making them more targeted. Furthermore, because the exception handling template uniformly indicates the log level, log content placeholders, insertion method of tracking identifiers, and exception recovery strategy, the inserted exception handling code and log handling code maintain consistency and standardization in log importance classification, log field structure, and execution control methods after an exception. This results in a more unified style, which is beneficial for improving the readability and traceability of exception or log information, facilitating effective retrieval and correlation analysis of log information, and reducing the impact of exceptions on system operation through recovery strategies. This, in turn, improves the stability and maintainability of code in incremental development scenarios.

[0119] In one possible implementation, historical project code can be obtained. Multiple anomaly type samples, corresponding exception handling code samples, and corresponding log handling code samples are identified within the historical project code. Statistical analysis is performed on these samples to obtain high-frequency exception handling code and high-frequency log handling code for each anomaly type. An anomaly handling template is then generated based on these high-frequency exception handling and log handling codes.

[0120] The historical project code refers to the collection of source code for projects that have been developed and put into use or archived. The exception type samples, exception handling code samples, and log handling code samples are sample data identified from the historical project code. High-frequency exception handling code samples are exception handling code samples whose frequency exceeds a frequency threshold, and high-frequency log handling code samples are log handling code samples whose frequency exceeds a frequency threshold.

[0121] High-frequency exception handling code and high-frequency log handling code can be used directly as references to generate exception handling templates. Alternatively, natural language processing technology can be used to identify code patterns in high-frequency exception handling code and high-frequency log handling code, and exception handling templates can be generated based on these patterns.

[0122] Therefore, by statistically analyzing sample data from historical project code and generating exception handling templates based on high-frequency exception handling code and high-frequency log handling code, the exception handling templates are derived from actual project code and can more realistically reflect the handling methods under common exception scenarios. This improves the rationality and standardization of exception handling code and log handling code generation, which is conducive to enhancing the consistency and maintainability of the code in exception handling. At the same time, it can be iteratively updated, improving the automation level of the code enhancement process.

[0123] In one possible implementation, the log transmission strategy can be selected based on the runtime environment.

[0124] Specifically, obtain the runtime environment of the current code.

[0125] If the runtime environment is a development environment, the log processing code includes a log transmission strategy that outputs log information to the local console.

[0126] If the runtime environment is a test environment, the log processing code includes a log transmission strategy of writing log information to a local log file and uploading it to the log server in batches via an asynchronous task.

[0127] If the operating environment is a production environment, the log processing code includes a log transmission strategy that uploads log information to the log server in real time.

[0128] Therefore, by selecting different log transmission strategies based on the current code's runtime environment, it's possible to adapt to actual use cases. In the development environment, logs are output only to the local console, facilitating quick viewing and debugging for developers and reducing unnecessary log reporting overhead. In the testing environment, log information is written to a local log file and uploaded to the log server in batches via asynchronous tasks, ensuring the integrity of log data retention while avoiding frequent uploads that could interfere with the testing process. In the production environment, real-time uploading of log information to the log server helps in the timely detection and localization of online anomalies, thereby improving the effectiveness and traceability of log recording while ensuring server performance.

[0129] In one possible implementation, if the log processing code is used to generate tracking identifiers, the enhanced code can be validated after generation to obtain a validation result. If the validation result indicates that the enhanced code is missing or non-compliant, an updated exception handling code or updated log processing code is generated through a pre-trained code refactoring model and merged into the enhanced code.

[0130] The verification includes at least one of the following verification methods:

[0131] (1) Check whether the corresponding exception handling code and log handling code are inserted at the location of the exception point.

[0132] (2) Check whether the tracking identifier generated by the log processing code is unique.

[0133] (3) Verify whether the log processing code overrides the corresponding context variables.

[0134] Among them, the verification result is the judgment information obtained after verifying the enhanced code, the pre-trained code refactoring model is a model trained through a large number of code samples, used to automatically generate or correct the code structure, and the context variables are parameters or state information related to the current business logic at the location where the exception occurs.

[0135] Therefore, by validating the generated enhanced code and automatically updating and merging the relevant code using a pre-trained code refactoring model when missing or non-compliant exception handling or logging code is found, the enhanced code has self-checking and correction capabilities. It can promptly detect and fix problems such as exception points not being correctly inserted into the processing logic, tracking identifiers not meeting uniqueness requirements, or log fields not fully covering context variables. This improves the completeness and standardization of exception handling and logging, reduces the workload of manual inspection and correction, enhances the traceability and accuracy of exception logs, and helps improve the stability of the system in actual operation and the efficiency of subsequent maintenance.

[0136] In addition to the above-described method embodiments, this application also provides an application scenario for applying the above-described method embodiments. In this application scenario, the existing code file is first parsed. By constructing an abstract syntax tree from the source code, the function call structure is identified, thereby locating the database query interface call statement, such as the db.query() call. This call statement is marked as an exception point that may cause an exception. Furthermore, the possible exception point types corresponding to this exception point are identified, such as ConnectionError or SQLSyntaxError.

[0137] After identifying the location and type of anomalies, a match is performed in the anomaly pattern library based on the anomaly type to determine the corresponding target anomaly handling template, such as a database anomaly template. The rules for this template can include the following structured configuration: {

[0138] "log_level": "error",

[0139] "log_content": "Database query failed",

[0140] "context_fields": ["query", "params"],

[0141] "report_method": "async_upload"

[0142] }

[0143] Based on the target exception handling template, exception handling code and logging code are generated in the draft code and inserted at the exception point. The corresponding code structure is as follows: try {

[0144] const result = await db.query(sql, params);

[0145] } catch (err) { / / Exception handling code

[0146] const traceId = generateTraceId(); / / Statement to generate trace identifier

[0147] logger.error({

[0148] traceId,

[0149] message: "Database query failed",

[0150] params,

[0151] Error: err.stack

[0152] });

[0153] reportError(traceId, err); / / Log processing code

[0154] throw err;

[0155] }

[0156] After the enhanced code is generated, it can be validated. Validation includes checking for the presence of a traceId, the setting of log levels, and the complete coverage of necessary context fields. If the validation results indicate missing fields (e.g., the params parameter is not recorded) or non-compliance with specifications, a pre-trained code model can be invoked to complete the missing parts. The completed code is then merged into the enhanced code to obtain the final version.

[0157] At the system architecture level, this application scenario can include three parts: client, server, and log server. The client is used to submit code requirements and display the generated results; the server is used to complete context parsing, anomaly identification, anomaly pattern matching, log instrumentation, and enhanced code verification and callback; the log server is used to receive log data generated by the enhanced code during actual operation for subsequent anomaly location and problem investigation.

[0158] See Figure 2 , Figure 2 A code processing apparatus 200 provided in this application embodiment includes:

[0159] The acquisition unit 201 is used to acquire newly added target code for the current code and the requirement description information corresponding to the target code, wherein the target code is code generated by a code generation model based on the requirement description information;

[0160] The parsing unit 202 is used to parse the requirement description information corresponding to the current code and the target code to obtain the structural information of the current code and the functional intent corresponding to the requirement description information;

[0161] The determining unit 203 is used to determine the location and type of the anomaly point based on the structural information and the functional intent.

[0162] The insertion unit 204 is used to insert exception handling code and log handling code into the draft code according to the location and type of the exception point before generating the draft code, so as to obtain enhanced code. The draft code includes the current code and the target code.

[0163] As one possible implementation, the log processing code includes trace identifier generation code, which generates a trace identifier that uniquely identifies the request of the enhancement code at the anomaly point location.

[0164] As one possible implementation, the insertion unit is specifically used for:

[0165] Based on the anomaly type, a target anomaly handling template corresponding to the anomaly type is determined. The anomaly handling template is used to indicate the log level, log content placeholder, insertion method of tracking identifier, and anomaly recovery strategy. The target anomaly handling template is one of multiple anomaly handling templates.

[0166] Based on the target exception handling template, generate the exception handling code and the log handling code;

[0167] The exception handling code and the log handling code are inserted into the draft code to obtain the enhanced code.

[0168] As one possible implementation, the device 200 further includes a template unit for:

[0169] Retrieve historical project code;

[0170] Identify multiple anomaly point type samples, anomaly handling code samples corresponding to the multiple anomaly point types, and log processing code samples corresponding to the multiple anomaly point types in the historical project code.

[0171] Statistical analysis was performed on multiple anomaly type samples, anomaly handling code samples corresponding to multiple anomaly types, and log processing code samples corresponding to multiple anomaly types to obtain high-frequency anomaly handling codes and high-frequency log processing codes corresponding to the multiple anomaly type samples.

[0172] The exception handling template is generated based on the high-frequency exception handling code and the high-frequency log handling code.

[0173] As one possible implementation, the device 200 further includes a log transmission unit for:

[0174] Obtain the runtime environment of the current code;

[0175] If the operating environment is a development environment, then the log transmission strategy included in the log processing code is to output log information through the local console;

[0176] If the operating environment is a test environment, the log transmission strategy included in the log processing code is to write the log information into a local log document and upload it to the log server in batches via an asynchronous task;

[0177] If the operating environment is a production environment, then the log transmission strategy included in the log processing code is to upload the log information to the log server in real time.

[0178] As one possible implementation, if the log processing code is used to generate a tracking identifier, then after generating the enhanced code, the device 200 further includes a verification unit for:

[0179] The enhanced code is verified to obtain the verification result of the enhanced code;

[0180] If the verification result indicates that the enhanced code is missing or non-compliant, the update exception handling code or update log handling code is generated by the pre-trained code reconstruction model and merged into the enhanced code;

[0181] The verification includes at least one of the following verification methods:

[0182] Check whether the corresponding exception handling code and log handling code are inserted at the location of the anomaly;

[0183] Check whether the tracking identifier generated by the log processing code is unique;

[0184] Verify whether the log processing code overrides the corresponding context variables.

[0185] See Figure 3 This application also provides a computer device, which includes a memory 301 and a processor 302.

[0186] The memory is used to store computer programs and to transfer the computer programs to the processor;

[0187] The processor is used to execute the method of the above method embodiment according to the computer program.

[0188] This application also provides a computer-readable storage medium, characterized in that the computer-readable storage medium is used to store a computer program, the computer program being used to execute the method of the above-described method embodiments.

[0189] This application also provides a computer program product including a computer program, which, when run on a computer device, causes the computer device to perform the method described in the above method embodiments.

[0190] It should be noted that the various embodiments in this specification are described in a progressive manner, with each embodiment focusing on the differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. For the systems or apparatus disclosed in the embodiments, since they correspond to the methods disclosed in the embodiments, the descriptions are relatively simple, and relevant parts can be referred to the method section.

[0191] The term "comprising" and its variations as used herein are open-ended inclusions, meaning "including but not limited to". The term "based on" means "at least partially based on". The term "one embodiment" means "at least one embodiment"; the term "another embodiment" means "at least one additional embodiment"; the term "some embodiments" means "at least some embodiments". Definitions of other terms will be given in the description below.

[0192] It should be understood that in this application, "at least one (item)" means one or more, and "more than" means two or more. "And / or" is used to describe the relationship between related objects, indicating that three relationships can exist. For example, "A and / or B" can represent three cases: only A exists, only B exists, and both A and B exist simultaneously, where A and B can be singular or plural. The character " / " generally indicates that the preceding and following related objects are in an "or" relationship. "At least one (item) of the following" or similar expressions refer to any combination of these items, including any combination of single or plural items. For example, at least one (item) of a, b, or c can represent: a, b, c, "a and b", "a and c", "b and c", or "a and b and c", where a, b, and c can be single or multiple.

[0193] It should also be noted that, in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.

[0194] The steps of the methods or algorithms described in conjunction with the embodiments disclosed herein can be implemented directly by hardware, a software module executed by a processor, or a combination of both. The software module can be located in random access memory (RAM), main memory, read-only memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, registers, hard disk, removable disk, CD-ROM, or any other form of storage medium known in the art.

[0195] The above description of the disclosed embodiments enables those skilled in the art to make or use this application. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of this application. Therefore, this application is not to be limited to the embodiments shown herein, but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.

Claims

1. A code processing method, characterized in that, The method includes: Obtain the target code newly added for the current code and the requirement description information corresponding to the target code, wherein the target code is code generated by the code generation model based on the requirement description information; The requirement description information corresponding to the current code and the target code is parsed to obtain the structural information of the current code and the functional intent corresponding to the requirement description information; Based on the structural information and the functional intent, determine the location and type of the anomaly. Before generating draft code, exception handling code and log handling code are inserted into the draft code according to the location and type of the exception, resulting in enhanced code. The draft code includes the current code and the target code.

2. The method according to claim 1, characterized in that, The log processing code includes trace identifier generation code, which is used to generate a trace identifier, and the trace identifier is used to uniquely identify the request of the enhancement code at the anomaly point location.

3. The method according to claim 2, characterized in that, The enhanced code is obtained by inserting exception handling code and log handling code into the draft code based on the location and type of the anomaly, including: Based on the anomaly type, a target anomaly handling template corresponding to the anomaly type is determined. The anomaly handling template is used to indicate the log level, log content placeholder, insertion method of tracking identifier, and anomaly recovery strategy. The target anomaly handling template is one of multiple anomaly handling templates. Based on the target exception handling template, generate the exception handling code and the log handling code; The exception handling code and the log handling code are inserted into the draft code to obtain the enhanced code.

4. The method according to claim 3, characterized in that, The method further includes: Retrieve historical project code; Identify multiple anomaly point type samples, anomaly handling code samples corresponding to the multiple anomaly point types, and log processing code samples corresponding to the multiple anomaly point type samples in the historical project code. Statistical analysis is performed on multiple anomaly type samples, anomaly handling code samples corresponding to multiple anomaly type samples, and log processing code samples corresponding to multiple anomaly type samples to obtain high-frequency anomaly handling codes and high-frequency log processing codes corresponding to the multiple anomaly type samples. The exception handling template is generated based on the high-frequency exception handling code and the high-frequency log handling code.

5. The method according to claim 1, characterized in that, The method further includes: Obtain the runtime environment of the current code; If the operating environment is a development environment, then the log transmission strategy included in the log processing code is to output log information through the local console; If the operating environment is a test environment, the log transmission strategy included in the log processing code is to write the log information into a local log document and upload it to the log server in batches via an asynchronous task; If the operating environment is a production environment, then the log transmission strategy included in the log processing code is to upload the log information to the log server in real time.

6. The method according to claim 1, characterized in that, If the log processing code is used to generate a tracking identifier, then after generating the enhanced code, the method further includes: The enhanced code is verified to obtain the verification result of the enhanced code; If the verification result indicates that the enhanced code is missing or non-compliant, the update exception handling code or update log handling code is generated by the pre-trained code reconstruction model and merged into the enhanced code; The verification includes at least one of the following verification methods: Check whether the corresponding exception handling code and log handling code are inserted at the location of the anomaly; Check whether the tracking identifier generated by the log processing code is unique; Verify whether the log processing code overrides the corresponding context variables.

7. A code processing device, characterized in that, The device includes: The acquisition unit is used to acquire newly added target code for the current code and the requirement description information corresponding to the target code, wherein the target code is code generated by a code generation model based on the requirement description information; The parsing unit is used to parse the requirement description information corresponding to the current code and the target code to obtain the structural information of the current code and the functional intent corresponding to the requirement description information; The determining unit is used to determine the location and type of the anomaly point based on the structural information and the functional intent. An insertion unit is used to insert exception handling code and log handling code into the draft code before generating draft code, based on the location and type of the exception point, to obtain enhanced code. The draft code includes the current code and the target code.

8. A computer device, characterized in that, The computer device includes a processor and memory: The memory is used to store computer programs and to transfer the computer programs to the processor; The processor is configured to perform the method according to any one of claims 1-6 according to the computer program.

9. A computer-readable storage medium, characterized in that, The computer-readable storage medium is used to store a computer program for performing the method according to any one of claims 1-6.

10. A computer program product comprising a computer program, characterized in that, When it is run on a computer device, it causes the computer device to perform the method described in any one of claims 1-6.