Integration between Sencha ExtJS and NGRest server

38 views
Skip to first unread message

Henry Nguyen

unread,
Jul 31, 2017, 7:23:14 PM7/31/17
to ngrest
Hi Dmitry,

I have a question on the error message return from the ngrest server to the client. However, there is nothing in the response when the error raised in the server as the Sencha ExtJS client http response  is undefined and when debugging the client the error message is caught (it seems like timing issue or the error does not return in the http response). What is ngrest error mechansim? Do you send the error message back to the client using http response.? Should I use NGREST_THROW_HTTP or something else I wonder?

Thanks and Regards, Henry

User user;
try {
user = LXSDb::inst().getTable<User>().selectOne("username = ?", username);
}
catch (const ::ngrest::Exception& exception) {
::ngrest::LogError() << "Exception: \n" << exception.what();
NGREST_THROW_HTTP(exception.what(), ngrest::HTTP_STATUS_500_INTERNAL_SERVER_ERROR);
}


if (user.password == password) {
ngrest::LogInfo() << "Login server successfully";
return (int) LXSDb::inst().getTable<Session>().insert({0, user.username, user.password}).lastInsertId();
} else {
ngrest::LogInfo() << "Failed to Login server";
NGREST_THROW_HTTP("Failed to login", ngrest::HTTP_STATUS_500_INTERNAL_SERVER_ERROR);
return 0;
}


Session login;
try {
login = LXSDb::inst().getTable<Session>().selectOne("username = ?", username);
} //NGREST_CATCH_ALL
catch (const ::ngrest::Exception& exception) {
::ngrest::LogError() << "Exception: \n" << exception.what();
NGREST_THROW_HTTP(exception.what(), ngrest::HTTP_STATUS_500_INTERNAL_SERVER_ERROR);
}

if(login.username == username && login.password == password) {
//NGREST_ASSERT_HTTP(login.username == username, ngrest::HTTP_STATUS_500_INTERNAL_SERVER_ERROR, "Duplicated login session" );
NGREST_THROW_HTTP("Duplicated login session", ngrest::HTTP_STATUS_500_INTERNAL_SERVER_ERROR);
}

Henry Nguyen

unread,
Jul 31, 2017, 10:05:55 PM7/31/17
to ngrest
Hi Dmitry,

How do I set the network timing so that the http response in the event of exception will be sent to the client after the http request.

At the moment I strongly believe that the server delay to the http response with error message as in my client I receive null pointer on the http response.

Thanks and Regards, Henry

Henry Nguyen

unread,
Aug 6, 2017, 7:45:05 PM8/6/17
to ngrest
Hi Dmitry,

This is one of the important part of error handling between client and server. My gut tells me the ClientHandler or Ngrest-Mod dont play nice with the Client as Client and Rest Java are working well. At the moment I can only work on the failure sending from the ngrest server to client but I can not analyse the error messages in the httpresponse.

I will generate an example to illustrate the issue for u. Give me few hours to cut down the application to GPL library usage...

Regards, Henry

Dmitry Utkin

unread,
Aug 9, 2017, 1:09:54 PM8/9/17
to ngrest
At this moment server does not limit response with timeout. This need to be done..

Any exceptions you throw will be sent as text with content-type: text/plain.
I just checked it and it works.

If you need to provide some mechanism of error handling, probably it would be better if you make base response class where you place Nullable error code and description (depending of your needs). 
Inherit all your responses from that class and set either of response or error accordingly.


for example:


struct Error
{
    int code;
    std::string description;
};

struct Result
{
    ngrest::Nullable<Error> error;

    template<typename ResultType>
    static ResultType err(Error error)
    {
        ResultType res;
        res.error = error;
        return res;
    }
};

struct Test432: public Result
{
    ngrest::Nullable<bool> res;
};


Test432 TestService::get() const
{
    if (itsOk) {
        Test432 res;
        res.res = true;
        return res;
    }

    return Result::err<Test432>(Error{1, "error #1"});
}



вторник, 1 августа 2017 г., 2:23:14 UTC+3 пользователь Henry Nguyen написал:
Message has been deleted
Message has been deleted
Message has been deleted

Henry Nguyen

unread,
Aug 10, 2017, 2:20:01 AM8/10/17
to ngrest
Hi Dmitry,

I can see the error text passing to the client side. However, the error does not return in the response immediately after the rest is executed. It returns to the client but then out of sync.

You can access to the test application at AWS. If you turn the inspect on the chrome you will see as I put in the debugger.


The username is admin and the password is admin for successful login.
Other, nothing or gubodygup string, will fail.

I attach the code for you as well in case you want to look closely

Thanks and Regards, Henry
login.db
sessions.so
Message has been deleted
Message has been deleted
Message has been deleted

Dmitry Utkin

unread,
Aug 10, 2017, 1:38:30 PM8/10/17
to ngrest
Sometimes google groups finds something bad in attach and removes the messages. In this case try to pack your files with 7zip encrypting the archive and the file names as well. place it here, providing the password.


Unfortunately I cannot get access to your server:

lxs.extel-iotlcoud.com’s server DNS address could not be found.

DNS_PROBE_FINISHED_NXDOMAIN


Just to be sure, do you use latest version of ngrest? there was a bug with large headers with recent Chrome versions.



Try to use curl or some plugin for chrome like Restlet Client to test your requests. It it does work, probably this is some sencha -specific problem.


четверг, 10 августа 2017 г., 9:20:01 UTC+3 пользователь Henry Nguyen написал:

Henry Nguyen

unread,
Aug 10, 2017, 8:39:39 PM8/10/17
to ngrest
Hi Dmitry,

My apology. It is

http://lxs.extel-iotcloud.com:8080

Regards, Henry

The issue is that the client completed the API but there is no error message in the httpresponse.

The error message came later on. I dont know when but the same message raised by the database such as Error excecuting query: no more rows.
Message has been deleted
Message has been deleted

Henry Nguyen

unread,
Aug 10, 2017, 8:45:11 PM8/10/17
to ngrest
Hi Dmitry,


Here is the client application - a smaller version with GPL.

Please look at the ons

onLoginFaiure

I am expecting httpresponse = operation.getResponse(); //undefined
message = httpresponse.responseText; //null


Regards, Henry
Message has been deleted
Message has been deleted
Message has been deleted

Henry Nguyen

unread,
Aug 10, 2017, 8:55:20 PM8/10/17
to ngrest
client application resend again. Try to change the extension to doc but it is 7z file
html.doc

Henry Nguyen

unread,
Aug 10, 2017, 8:59:24 PM8/10/17
to ngrest
Hi Dmitry,

The file is html.doc (extension). It is actually html.7z

Just drop to the html (nginx or apache)
Turn on inspect in Chrome (I place the debugger there)

I assume that the db file and session.so are coming across ok.

Regards, Henry

Dmitry Utkin

unread,
Aug 11, 2017, 4:29:13 PM8/11/17
to ngrest
Unfortunately sessions.so cannot be loaded into ngrest server:

W/11-08-2017 23:15:54.118 Can't load service: /home/dem/.ngrest/ngrest-build/deploy/share/ngrest/services/sessions.so: Failed to load library [/home/dem/.ngrest/ngrest-build/deploy/share/ngrest/services/sessions.so]: /home/dem/.ngrest/ngrest-build/deploy/share/ngrest/services/sessions.so: undefined symbol: _ZN6ngrest13HttpExceptionC1EPKcS2_RKSsNS_10HttpStatusE

$ c++filt _ZN6ngrest13HttpExceptionC1EPKcS2_RKSsNS_10HttpStatusE
ngrest::HttpException::HttpException(char const*, char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, ngrest::HttpStatus)



Looks like my STL version differs from yours because my libs are built like that:

$ nm serverstatus.so | grep HttpStatus
                 U _ZN6ngrest13HttpExceptionC1EPKcS2_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS_10HttpStatusE

$ c++filt _ZN6ngrest13HttpExceptionC1EPKcS2_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS_10HttpStatusE
ngrest::HttpException::HttpException(char const*, char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, ngrest::HttpStatus)




From your site I get instantly logged in and can see this after entering login/password your provided. I don't see any freezes.. Please see screenshots attached




пятница, 11 августа 2017 г., 3:59:24 UTC+3 пользователь Henry Nguyen написал:
login1.png
login3.png
login2.png

Dmitry Utkin

unread,
Aug 11, 2017, 4:52:03 PM8/11/17
to ngrest
Regarding freeze on error response, I believe it's the problem with client framework you use.

I just injected jQuery and few lines of code into your site using Chrome User JavaScript and CSS extension.

the lines are:

console.log("start: " + new Date());
jQuery.post("/myapp/Sessions", {"username":"asdqdqw","password":"123123"})
    .done(function(data) {
        console.log("login success:" + JSON.stringify(data, null, 2));
        console.log("end: " + new Date());
    })
    .fail(function(data) {
        console.error("ERROR: " + JSON.stringify(data, null, 2));
        console.log("end: " + new Date());
    });


It invokes AJAX POST request with invalid data like you sent to simulate the freeze. But it receive the result correctly within the same second. Please see screenshot in attach for the log.


Please try jQuery with this snipped and check if it works.


req-jquery.png

Dmitry Utkin

unread,
Aug 11, 2017, 5:09:29 PM8/11/17
to ngrest
Oops, wrong query, the correct one:


console.log("start: " + new Date());
jQuery.ajax("/myapp/Sessions",  {
      data: JSON.stringify({"username":"asdqdqw","password":"123123"}),
      contentType: 'application/json',
      type: 'POST'
})
.done(function(data) {
    console.log("login success:" + JSON.stringify(data, null, 2));
    console.log("end: " + new Date());
})
.fail(function(data) {
    console.error("ERROR: " + JSON.stringify(data, null, 2));
    console.log("end: " + new Date());
});


And it still works, please see attach.


пятница, 11 августа 2017 г., 23:52:03 UTC+3 пользователь Dmitry Utkin написал:
req-jquery1.png

Henry Nguyen

unread,
Aug 13, 2017, 7:10:00 PM8/13/17
to ngrest
Hi Dmitry,

Here is the source for the session.so.

Thanks and Regards, Henry
loginapp.7z

Henry Nguyen

unread,
Aug 13, 2017, 7:20:45 PM8/13/17
to ngrest
Hi Dmitry,

Correct login admin/admin


"From your site I get instantly logged in and can see this after entering login/password your provided. I don't see any freezes.. Please see screenshots attached"

Failure login admin/jfdjjfdjjfdjfd or jfdjjkfjdjfjdjf/jfdjfkkddjk

If you turn on "inspect for Chrome or Inspect Element for Mozilla FIrefox" then you will see the debugger will kick in, then in the debugger windows you will the value return etc...


My C++ and STL is very rusty...I am programming using my instinct rather than going deep...


"
Looks like my STL version differs from yours because my libs are built like that:
"

It is likely that I am not using the Exception and/or ClientWrapper correctly - you can comment on the code. At the moment I can only use the failure in general but not being able to catch specific error return by the database and/or any future fine grained error if any. I will need to catch for any specific error to take appropriate action.

Thanks and Regards, Henry







Regards, Henry

Henry Nguyen

unread,
Aug 17, 2017, 1:08:52 AM8/17/17
to ngrest
Hi Dmitry,


"
If you need to provide some mechanism of error handling, probably it would be better if you make base response class where you place Nullable error code and description (depending of your needs). 
Inherit all your responses from that class and set either of response or error accordingly.
"
Please guide me through what you really mean when it comes to integrate the error to ngrest so that my error will transport to the client application. What would the ngrest exception semantic allow me to pass the error class instance to the exception?

Regards, Henry



On Thursday, August 10, 2017 at 3:09:54 AM UTC+10, Dmitry Utkin wrote:

Henry Nguyen

unread,
Aug 17, 2017, 2:30:56 AM8/17/17
to ngrest
Hi Dmitry,

My stdlibc++ details are:

$ ls -al /usr/lib64/libstdc++.so.6
lrwxrwxrwx. 1 root root 19 Jun 13 15:16 /usr/lib64/libstdc++.so.6 -> libstdc++.so.6.0.19


$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-4/root/usr/libexec/gcc/x86_64-redhat-linux/5.3.1/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-4/root/usr --mandir=/opt/rh/devtoolset-4/root/usr/share/man --infodir=/opt/rh/devtoolset-4/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --enable-plugin --with-linker-hash-style=gnu --enable-initfini-array --disable-libgcj --with-default-libstdcxx-abi=gcc4-compatible --with-isl=/builddir/build/BUILD/gcc-5.3.1-20160406/obj-x86_64-redhat-linux/isl-install --enable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 5.3.1 20160406 (Red Hat 5.3.1-6) (GCC)
[hlkn@localhost LXSscanners]$

What is yours?

Thanks and Regards, Henry

Henry Nguyen

unread,
Aug 17, 2017, 4:31:51 AM8/17/17
to ngrest
Hi Dmitry,

It seems that ExtJS is out of sync with the response message


    onLoginFailure: function(rec, operation) {
        debugger;
        var response = operation.getResponse().responseText, //operation.getResponse() = undefined and operation.getResponse().responseText = null
            obj = Ext.decode(response);


        Ext.Msg.alert('Login Failure', obj.msg)
    },


Regards, Henry

Dmitry Utkin

unread,
Aug 17, 2017, 3:46:48 PM8/17/17
to ngrest
So this is a problem with ExtJS side.. Probably it expects response in JSON format, but receives in text/plain and cannot decode it correctly.



четверг, 17 августа 2017 г., 11:31:51 UTC+3 пользователь Henry Nguyen написал:

Henry Nguyen

unread,
Aug 18, 2017, 1:37:04 AM8/18/17
to ngrest
Hi Dmitry,

Is there a way for the ngrest to generate error message in json format?

NGREST_THROW_HTTP("{\"msg\": \"Server - Failed to login due to incorrect password\"}", ngrest::HTTP_STATUS_500_INTERNAL_SERVER_ERROR);

or

NGREST_THROW_HTTP("{\"result\":{\"msg\": \"Server - Failed to login due to incorrect password\"}}", ngrest::HTTP_STATUS_500_INTERNAL_SERVER_ERROR);

or

use JSON library to generate the JSON error message. But then what is the schema for the error?

Regards, Henry

Dmitry Utkin

unread,
Aug 18, 2017, 4:02:38 PM8/18/17
to ngrest
ngrest error handling is intended for critical situations when your code (or library you use) throws exceptions.

If you need for some error handling mechanism you can implement it like I wrote before.

Inherit all your response from Result struct and return either of your result or error.




// place this into some shared header

struct Error
{
    int code;
    std::string description;
};

struct Result
{
    ngrest::Nullable<Error> error;

    template<typename ResultType>
    static ResultType err(Error error)
    {
        ResultType res;
        res.error = error;
        return res;
    }
};

// example of some returning struct

struct Test432: public Result
{
    ngrest::Nullable<bool> res;
};



// your operation


Test432 TestService::get() const
{
    if (itsOk) {
        Test432 res;
        res.res = true;
        return res;
    }

    return Result::err<Test432>(Error{1, "error #1"});
}






пятница, 18 августа 2017 г., 8:37:04 UTC+3 пользователь Henry Nguyen написал:
Reply all
Reply to author
Forward
0 new messages