iporaitech / pwr2-docker

ARCHIVED: Docker image based on Ubuntu with Elixir and Phoenix Framework integrated with Webpack+Relay+React
MIT License
20 stars 6 forks source link

Manage Auth errors #52

Open edipox opened 8 years ago

edipox commented 8 years ago

Currently when there is an auth error, for example an invalid access token. We have the following error:

Warning: RelayReadyState: Invalid state change from `{"aborted":false,"done":false,"error":{},"events":[{"type":"NETWORK_QUERY_START"},{"type":"CACHE_RESTORE_START"},{"type":"CACHE_RESTORE_FAILED"},{"type":"NETWORK_QUERY_ERROR","error":{}}],"ready":false,"stale":false}` to `{"error":{}}`.

Instead of this we should just redirect to login page with an explanatory message

valoricDe commented 8 years ago

I think we have to extend the DefaultNetworkLayer like:

export default class RelayNetworkLayer extends DefaultNetworkLayer {
    sendQueries(requests): Promise {
        return Promise.all(requests.map(request => (
            this._sendQuery(request).then(
                result => result.json()
            ).then(payload => {
                if (payload.hasOwnProperty('errors')) {
                    const error: any = new Error(
                        'Server request for query `' + request.getDebugName() + '` ' +
                        'failed for the following reasons:\n\n' +
                        this.formatErrors(request, payload.errors)
                    );
                    error.source = payload;
                    this.onError(error);
                    request.reject(error);
                } else if (!payload.hasOwnProperty('data')) {
                    const error = new Error(
                        'Server response was missing for query `' + request.getDebugName() +
                        '`.'
                    );
                    this.onError(error);
                    request.reject(error);
                } else {
                    request.resolve({ response: payload.data });
                }
            }).catch(
                error => {
                    this.onError(error);
                    request.reject(error);
                }
            )
        )));
    }
}

Then we could register onError and handle invalid tokens.