Show TOC

Message ParserLocate this document in the navigation structure

The message parser is a simple interface that is implemented to allow the propagation of messages from back end services. For messages from OData services, the sap.ui.model.ODataMessageParser is used.

The OData message parser is created automatically for all sap.ui.model.odata.v2.ODataModel instances and parses all responses from the server. The ODataModel implements the message processor interface and is used to propagate the messages to the message manager. In case of an error response, the response body is parsed for error messages. In case of a sucessful response, the "sap-message" header is parsed as a JSON-formatted error object. The name of the header field can be changed by calling the setHeaderField() method on the ODataMessageParser.

Implementing your Own Message Parser

If you have your own service implementation, for example, a JSON-based back end that also sends messages, you can implement your own message parser by implementing the sap.ui.core.message.MessageParser interface.The interface is very simple: It has only the parse and the setProcessor method. The parse method takes at least one parameter, that is, the response object from the server. The method can take more model-specific arguments. The setProcessor method takes only one argument, the processor object that is used to propagate the messages, this is usually the model instance.

The main task of the message parser is to retrieve the messages from the back end response and then calculate the message delta that is handed over to the message processor by means of the two parameters oldMessages and newMessages of the messageChange event. The oldMessages parameter specifies the messages that are to be removed, and the newMessages parameter specifies the messages that are to be added.

this.getProcessor().fireMessageChange({
    oldMessages: aRemovedMessages,
    newMessages: aNewMessages
});

The delta calculation must be a back end-specific implementation. In the OData implementation, for example, all messages for the requested resource(s) must be returned from the back end on every request. This means that all messages that were available before with a target that corresponds to the requested resources must be put in the oldMessages parameter of the event.