aquametalabs / aquameta

Web development platform built entirely in PostgreSQL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

datum: When a .function() call 500s, it doesn't display the error message

erichanson opened this issue · comments

For example:

GET https://localh0st:4443/endpoint/0.3/function/bundle/bundle_delete?args=%7B%22vals%22%3A%5B%222ff089e4-0f4e-4e7c-8408-b24ba19c8cdb%22%5D%7D&meta_data=true

This URL 500s at the moment. The response content is a JSON object that contains a description, but the error description not making it into the Javascript console. See also #234

Making the call:

w.find('button.merge').click(function() {
    if (confirm('Merging this commit will add all changes to this commit to the working copy.  Are you sure?')) {
        endpoint.schema('bundle').function('merge', [ commit.get('id') ]).then(function() {
	        w.trigger('reload_row_list');
			w.trigger('status', 'checked out commit ' + commit.get('id'));
            w.trigger('reload_tab_self');
        }).catch(function(e) {
            console.log('error message: ', e);
            alert ('merge failed: '+e.message);
        });
    }
});

The problem is that the e variable in the catch doesn't contain the full error message sent back by the server.

Here's the relevent datum.js code that dispatches the fetch and handles errors (located in datum.js line 382) in the org.aquameta.core.endpoint bundle.

            request = request.then(function(response) {

                // JSON was returned from WebSocket
                if (typeof response.json == 'undefined') {
                    // TODO: ? Unfortunately this has no HTTP status like the result of fetch
                    //console.log('i am the response', response);
                    return response;
                }

                // Request object was returned from fetch

                // Read json stream
                var json = response.json();

                if (response.status >= 200 && response.status < 300) {
                    return json;
                }

                // If bad request (code 300 or higher), reject promise
                return json.then(Promise.reject.bind(Promise));

            }).catch(function(error) {

                // Log error in collapsed group
                console.groupCollapsed(method, error.status_code, error.title);
                // console.error(url_without_query);
                if ('message' in error) {
                    console.error(error.message);
                }
                console.groupEnd();
                throw error.title;

            });

The response body is a nice JSON object:

{
  "status_code": 500,
  "title": "Server Error",
  "message": "PostgreSQL Exception:\n    state  : P0001\n    message: Merge not permitted when this bundle has uncommitted changes\n    detail : \n    hint   : \n    context: PL/pgSQL function bundle.merge(uuid) line 28 at RAISE\nPL/pgSQL function anonymous_rows_select_function(text,text,json) line 42 at FOR over EXECUTE statement\nPL/pgSQL function endpoint.request(text,text,text,json,json) line 160 at RETURN QUERY\n    sqlerr: Merge not permitted when this bundle has uncommitted changes\n    sqlstate: P0001"
}

I'd hate to check the blame on this, but there is no error.title. Maybe just try re-throwing the entire error in the catch handler in datum

Mission accomplished