Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/trn 28/improve network errors #83

Merged
merged 8 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oat-sa/tao-core-sdk",
"version": "1.7.0",
"version": "1.8.0",
"displayName": "TAO Core SDK",
"description": "Core libraries of TAO",
"homepage": "https://github.com/oat-sa/tao-core-sdk-fe#readme",
Expand Down
49 changes: 49 additions & 0 deletions src/core/error/ApiError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2020 (original work) Open Assessment Technologies SA ;
*/

import errorTypes from 'core/error/types';
oatymart marked this conversation as resolved.
Show resolved Hide resolved

/**
* Errors sent by HTTP API / backend
*/
//eslint-disable-next-line
export default class ApiError extends Error {

/**
* Instantiate an error
* @param {string} message - the error message
* @param {number} errorCode - the API error code
* @param {Object} response - the full response object
* @param {boolean} [recoverable=true] - can the user recover after having such error ?
* @param {...} params - additional error parameters (line, etc.)
*/
constructor(message, errorCode, response, recoverable = true, ...params) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks correct API-wise, but since there is no standard, I didn't see how we could ever benefit from the extra ...params passed through.

As you said, in Firefox they map onto properties like lineNumber, fileName, but it's proprietary, Safari doesn't do anything with such params.

super(message, ...params);

if (Error.captureStackTrace) {
Error.captureStackTrace(this, ApiError);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen in other implementations fallback to this.stack = (new Error()).stack if Error.captureStackTrace is not supported

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error.captureStackTrace is only used in node.js (so the stack trace appears in the unit tests when failing).
By default, the stack will be captured by inheriting from the Error type, it is generated by the constructor. The implementation should be identical cross browser once handled by Babel.


this.name = 'ApiError';
this.message = message;
this.errorCode = errorCode;
this.response = response;
this.recoverable = !!recoverable;
krampstudio marked this conversation as resolved.
Show resolved Hide resolved
this.type = errorTypes.api;
}
}
45 changes: 45 additions & 0 deletions src/core/error/AuthError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2020 (original work) Open Assessment Technologies SA ;
*/

import errorTypes from 'core/error/types';

/**
* Error due to client side authentication mechanisms
*/
//eslint-disable-next-line
export default class AuthError extends Error {

/**
* Instantiate an error
* @param {string} message - the error message
* @param {Boolean} [recoverable=true] - can the user recover after having such error ?
* @param {...} params - additional error parameters (line, etc.)
*/
constructor(message, recoverable = true, ...params) {
super(message, ...params);

if (Error.captureStackTrace) {
Error.captureStackTrace(this, AuthError);
}

this.name = 'AuthError';
this.message = message;
this.recoverable = !!recoverable;
this.type = errorTypes.auth;
}
}
49 changes: 49 additions & 0 deletions src/core/error/NetworkError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2020 (original work) Open Assessment Technologies SA ;
*/

import errorTypes from 'core/error/types';

/**
* Network errors
*/
//eslint-disable-next-line
export default class NetworkError extends Error {

/**
* Instantiate an error
* @param {string} message - the error message
* @param {number} [errorCode] - the HTTP status if any
* @param {Object} [response] - the full response object if any
* @param {boolean} [recoverable=true] - can the user recover after having such error ?
* @param {...} params - additional error parameters (line, etc.)
*/
constructor(message, errorCode, response, recoverable = true, ...params) {
super(message, ...params);

if (Error.captureStackTrace) {
Error.captureStackTrace(this, NetworkError);
}

this.name = 'NetworkError';
this.message = message;
this.errorCode = errorCode;
this.response = response;
this.recoverable = !!recoverable;
this.type = errorTypes.network;
}
}
45 changes: 45 additions & 0 deletions src/core/error/RenderingError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2020 (original work) Open Assessment Technologies SA ;
*/

import errorTypes from 'core/error/types';

/**
* Error in rendering
*/
//eslint-disable-next-line
export default class RenderingError extends Error {

/**
* Instantiate an error
* @param {string} message - the error message
* @param {Boolean} [recoverable=true] - can the user recover after having such error ?
* @param {...} params - additional error parameters (line, etc.)
*/
constructor(message, recoverable = true, ...params) {
super(message, ...params);

if (Error.captureStackTrace) {
Error.captureStackTrace(this, RenderingError);
}

this.name = 'RenderingError';
this.message = message;
this.recoverable = !!recoverable;
this.type = errorTypes.rendering;
}
}
47 changes: 47 additions & 0 deletions src/core/error/TimeoutError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2020 (original work) Open Assessment Technologies SA ;
*/

import errorTypes from 'core/error/types';

/**
* Error when an action times out
*/
//eslint-disable-next-line
export default class TimeoutError extends Error {

/**
* Instantiate an error
* @param {string} message - the error message
* @param {number} timeout - the timeout value
* @param {boolean} [recoverable=true] - can the user recover after having such error ?
* @param {...} params - additional error parameters (line, etc.)
*/
constructor(message, timeout, recoverable = true, ...params) {
super(message, ...params);

if (Error.captureStackTrace) {
Error.captureStackTrace(this, TimeoutError);
}

this.name = 'TimeoutError';
this.message = message;
this.timeout = timeout;
this.recoverable = !!recoverable;
this.type = errorTypes.timeout;
}
}
45 changes: 45 additions & 0 deletions src/core/error/UserError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2020 (original work) Open Assessment Technologies SA ;
*/

import errorTypes from 'core/error/types';

/**
* Error due to wrong user input
*/
//eslint-disable-next-line
export default class UserError extends Error {

/**
* Instantiate an error
* @param {string} message - the error message
* @param {boolean} [recoverable=true] - can the user recover after having such error ?
* @param {...} params - additional error parameters (line, etc.)
*/
constructor(message, recoverable = true, ...params) {
super(message, ...params);

if (Error.captureStackTrace) {
Error.captureStackTrace(this, UserError);
}

this.name = 'UserError';
this.message = message;
this.recoverable = !!recoverable;
this.type = errorTypes.user;
}
}
37 changes: 37 additions & 0 deletions src/core/error/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2020 (original work) Open Assessment Technologies SA ;
*/

export default Object.freeze({
// the server API is not successful: 500, 412, 403, etc.
api: 'api',

// any network error: CORS, offline, etc.
network: 'network',

// timeout error: an action cannot be performed in the given time
timeout: 'timeout',

// authentication: internal error about authentication (token pool issue, etc.)
auth: 'auth',

// errors due to user's input: wrong data range, etc.
user: 'user',

// rendering error: an interface, a component fails to render
rendering: 'rendering'
});
Loading