#1 2022-07-12 13:26:13

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 430

How to return unified error reporting

When using Interfaces for an API server, during processing Mormot2 returns an JSON object, like the following, describing Error:
{
    "errorCode": 406,
    "errorText": "IRestinterface.myprocedure failed parsing 2ndparameter: UTF8String from input JSON"
}

In an interface based rest call our code (inside the procedure/function called) will check the parameters/arguments provided if they are inside the accept range. If not an error should be returned.
If we want to have a custom response like the above, how can we do that?

Lets see the following interface function
function GetAsong(const SongId:integer; out song:TormSong):boolean;
begin
result:=db.orm.retrieve(SongId,song);
end;

if no song with the requested SongId exists, a message like the above Json object error will be a better solution to be returned, instead of an empty TormSong structure and a false value.

I tried to use the following:
ServiceRunningContext.Request.Call.OutBody:='';
ServiceRunningContext.Request.Call.OutStatus:=404;
ServiceRunningContext.Request.Error('%',['Song not found'],404);
but without success.

Is the above possible?
Thank you in advance

Offline

#2 2022-07-12 15:46:08

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: How to return unified error reporting

Don't use HTTP status codes for errors.
They are a technical detail about HTTP.
Use application specific errors.
See https://synopse.info/files/html/Synopse … l#TITL_165

When you use interface based services, you can't mix the raw returned content, unless there is no returned parameter so you can use TServiceCustomAnswer
https://synopse.info/files/html/Synopse … #TITLE_478

Offline

#3 2022-07-12 16:07:39

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 430

Re: How to return unified error reporting

Too complicated. I will stay with the empty TormSong json object in the response
Thank you a lot @ab

Offline

#4 2022-07-15 03:14:30

htits2008
Member
Registered: 2015-03-25
Posts: 34

Re: How to return unified error reporting

I have same error . I have interface service it work ok when use V1.18. and I turn to V2 ,It can't work . Something is  different?

 function upLoadFile(aTypeCode: rawUtf8; aTypeID: Integer;
      aFilename: rawUtf8; aFileStream: RawByteString; fileDate: TDateTime;
      aLoginID: Integer): Integer; 

AJAX:

			reader.readAsDataURL(file)
			reader.onload = (fd) => {    
                let fdd = fd.target.result.substring(fd.target.result.indexOf(',') + 1); // read from file by 
				axios({
                    method:     "POST",                       
                    url:       '/TypeAction.upLoadFile',
                    headers : {'Content-Type': 'multipart/form-data'} ,
					contentType: "application/json;charset=utf-8",
                    data:{					
                        aTypeCode :'pdf',
                        aTypeID : 0,
                        aFilename:'pdf0.pdf',     
						aFileStream: fdd,
						fileDate: Date(),
						aLoginID: 9
                    },
                    timeout:  8000
                }).then()......

The error info:
20220715 01495607 debug     mormot.rest.sqlite3.TRestServerDB(04e07b20) TRestServerRoutingRest.Error: {  "errorCode":406,  "errorText":"ITypeAction.upLoadFile failed parsing aFileStream: RawByteString from input JSON"  }

Last edited by htits2008 (2022-07-15 03:17:02)

Offline

#5 2022-07-15 06:32:44

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: How to return unified error reporting

IIRC the RawByteString parameter should be transmitted as base-64 encoded.
This is what is documented even in mORMOt 1.
https://synopse.info/files/html/Synopse … l#TITL_154
My guess is that mORMot 1 was more relaxed about this specification, whereas mORMot 2 is more strict.

OR

Mime multi-part is not handled the same as regular requests. In your code, you have both multipart and JSON, which are not compatible.
Perhaps the client code is mixing things up.

Try to debug a little more and see what JSON is actually transmitted to the mORMot server.

Offline

#6 2022-07-15 06:58:16

htits2008
Member
Registered: 2015-03-25
Posts: 34

Re: How to return unified error reporting

I found base-64 encoded strings in server's log.
20220715 06444743 call      mormot.rest.sqlite3.TRestServerDB(01742b20) ITypeAction.upLoadFile{"aTypeCode":"pdf","aTypeID":0,"aFilename":"pdf0.pdf","aFileStream":"JVBERi0xLjcNCiWhs8XXDQo3IDAgb2JqDQo8PC9UeXB ............
20220715 06444743 debug     mormot.rest.sqlite3.TRestServerDB(01742b20) TRestServerRoutingRest.Error: {  "errorCode":406,  "errorText":"ITypeAction.upLoadFile failed parsing aFileStream: RawByteString from input JSON"  }
20220715 06444743 srvr      xxx  POST orp/TypeAction.upLoadFile Interface=406 out=120 B in 4.29ms

but it can run to my code in upLoadFile function.

Offline

#7 2022-07-15 07:38:18

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: How to return unified error reporting

There was an inconsistency with mORMot 2.
It expected a base-64 trailer, which was not required in mORMot 1.

Please try https://github.com/synopse/mORMot2/commit/cc6e7a49

Offline

#8 2022-07-19 08:46:51

htits2008
Member
Registered: 2015-03-25
Posts: 34

Re: How to return unified error reporting

I find the way . It can run.

aFileStream:rawUtf8;
FFileStream:= Base64ToBin(aFileStream);   

Offline

Board footer

Powered by FluxBB