cancel
Showing results for 
Search instead for 
Did you mean: 

Display OData error message in bindelement( )

thomas_arnesen
Explorer
0 Kudos

Hi there

I've a problem with OData where I can't access the error message when it goes wrong.

My controller has the following code - it all works fine.

onInit: function(){
	this.getRouter().getRoute("display").attachPatternMatched(this.onObjectMatched, this);
},
onObjectMatched: function(oEvent){
	var sObjectPath = this.oModel.createKey("DataSet", {
		Var1: oEvent.getParameter("arguments").Var1,
		Var2: oEvent.getParameter("arguments").Var2
	});
	this.getView().bindElement({
		path: "/" + sObjectPath,
		events: {
			change: this.onBindingChange.bind(this)
		}
	});
},
onBindingChange: function(){
	var oElementBinding = this.getView().getElementBinding();
	if (!oElementBinding.getBoundContext()) {
		this.getRouter().getTargets().display("notFound");
	}
}

If the bindElement fails (because I manipulate the URL to access something I shouldn't, for example) I get an error in the console.

This error of course has an error message. My question is; how could I access this error message in the onBindingChange method? Is it possible? Or, even, anywhere (I could store it in the UIHelper if need be).

I've been looking all over Google and here but have not gotten nowhere.

In the bindElement method, I've tried to add the dataReceived event but that returns nothing. I've tried the attachRequestFailed on the oModel but that didn't work either.

Any help appreciated,
Thomas

Accepted Solutions (0)

Answers (4)

Answers (4)

karstenvoigt
Participant
0 Kudos

You can check (if using OData v4):

https://help.sap.com/doc/saphelp_nw751abap/7.51.0/de-DE/5d/e13cf4dd1f4a3480f7e2eaaee3f5b8/frameset.h...

With OData v4 many events are just not supported.

Something like the following snippet could help:

this.getView().bindElement({
	path: "/" + sObjectPath,
	events: {
		change: this.onBindingChange.bind(this),
                dataReceived: function (oEvent) {
			if (oEvent.getParameter("error")) {
				// error handling
			} else {
                                // something useful
                        }
	}
});

Regards

Karsten

saxos
Explorer
0 Kudos

Hi,

even though you're saying its not working for you, it is working for me.

you can add this to your Odata-Service:

var oModel = this.getView().getModel("myPrettyOdataModel");

oModel.attachRequestFailed(function (response) {
                this.makeTheErrorInSomethingNice(response);
   }, this);

Maybe you made a mistake somewhere?

Regards,

Samuel

karstenvoigt
Participant
0 Kudos

This depends on your used OData version. For v2 everything is fine; but with v4 many events are not supported.

https://help.sap.com/doc/saphelp_nw751abap/7.51.0/de-DE/5d/e13cf4dd1f4a3480f7e2eaaee3f5b8/frameset.h...

0 Kudos

Hi, did you find solution?

We have similar request with PO - it may be that header details are returned, but if a specific item properties exist - it is not allowed to change PO - in this case I get error message in console - how to display it to user?

thomas_arnesen
Explorer
0 Kudos

Hi Shanir

I haven't found a solution for this yet. Other projects have taken my time but I'll revisit this at some point and if I do find a solution I'll update here.

former_member484715
Contributor
0 Kudos

Hi Thomas Arnesen,

You can go through this link. It is specified there that, the bindElement method has an parameter events. You can define an custom event here, to detect wether your model path is correct or not.

Regards,

Arjun Biswas.