MOBILE-4670 error: Allow specifying error title in CoreError

main
Dani Palou 2024-12-04 12:07:58 +01:00
parent 04727defa2
commit dc7bea0b2e
4 changed files with 22 additions and 18 deletions

View File

@ -24,20 +24,29 @@ import { CoreErrorObject } from '@services/error-helper';
*/ */
export class CoreError extends Error { export class CoreError extends Error {
title?: string;
debug?: CoreErrorDebug; debug?: CoreErrorDebug;
constructor(message?: string, debug?: CoreErrorDebug) { constructor(message?: string, options: CoreErrorOptions = {}) {
super(message); super(message);
// Fix prototype chain: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#support-for-newtarget // Fix prototype chain: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#support-for-newtarget
this.name = new.target.name; this.name = new.target.name;
Object.setPrototypeOf(this, new.target.prototype); Object.setPrototypeOf(this, new.target.prototype);
this.debug = debug; this.title = options.title;
this.debug = options.debug;
} }
} }
export type CoreErrorOptions = {
// Error title. By default, 'Error'.
title?: string;
// Debugging information.
debug?: CoreErrorDebug;
};
/** /**
* Debug information of the error. * Debug information of the error.
*/ */

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
import { CoreError, CoreErrorDebug } from '@classes/errors/error'; import { CoreError, CoreErrorOptions } from '@classes/errors/error';
import { CoreUserSupportConfig } from '@features/user/classes/support/support-config'; import { CoreUserSupportConfig } from '@features/user/classes/support/support-config';
/** /**
@ -20,13 +20,11 @@ import { CoreUserSupportConfig } from '@features/user/classes/support/support-co
*/ */
export class CoreSiteError extends CoreError { export class CoreSiteError extends CoreError {
debug?: CoreErrorDebug;
supportConfig?: CoreUserSupportConfig; supportConfig?: CoreUserSupportConfig;
constructor(options: CoreSiteErrorOptions) { constructor(options: CoreSiteErrorOptions) {
super(options.message); super(options.message, { title: options.title, debug: options.debug });
this.debug = options.debug;
this.supportConfig = options.supportConfig; this.supportConfig = options.supportConfig;
} }
@ -43,12 +41,9 @@ export class CoreSiteError extends CoreError {
} }
export type CoreSiteErrorOptions = { export type CoreSiteErrorOptions = CoreErrorOptions & {
message: string; message: string;
// Debugging information.
debug?: CoreErrorDebug;
// Configuration to use to contact site support. If this attribute is present, it means // Configuration to use to contact site support. If this attribute is present, it means
// that the error warrants contacting support. // that the error warrants contacting support.
supportConfig?: CoreUserSupportConfig; supportConfig?: CoreUserSupportConfig;

View File

@ -286,10 +286,10 @@ export class CoreSitesProvider {
siteUrl = CoreUrl.formatURL(siteUrl); siteUrl = CoreUrl.formatURL(siteUrl);
if (!CoreUrl.isHttpURL(siteUrl)) { if (!CoreUrl.isHttpURL(siteUrl)) {
throw new CoreError(Translate.instant('core.login.invalidsite'), { throw new CoreError(Translate.instant('core.login.invalidsite'), { debug: {
code: 'invalidprotocol', code: 'invalidprotocol',
details: `URL contains an invalid protocol when checking site.<br><br>Origin: ${origin}.<br><br>URL: ${siteUrl}.`, details: `URL contains an invalid protocol when checking site.<br><br>Origin: ${origin}.<br><br>URL: ${siteUrl}.`,
}); } });
} }
if (!CoreNetwork.isOnline()) { if (!CoreNetwork.isOnline()) {

View File

@ -52,10 +52,10 @@ export class CoreCustomURLSchemesProvider {
* @returns Error. * @returns Error.
*/ */
protected createInvalidSchemeError(url: string, data?: CoreCustomURLSchemesParams): CoreCustomURLSchemesHandleError { protected createInvalidSchemeError(url: string, data?: CoreCustomURLSchemesParams): CoreCustomURLSchemesHandleError {
const defaultError = new CoreError(Translate.instant('core.login.invalidsite'), { const defaultError = new CoreError(Translate.instant('core.login.invalidsite'), { debug: {
code: 'invalidurlscheme', code: 'invalidurlscheme',
details: `Error when treating a URL scheme, it seems the URL is not valid.<br><br>URL: ${url}`, details: `Error when treating a URL scheme, it seems the URL is not valid.<br><br>URL: ${url}`,
}); } });
return new CoreCustomURLSchemesHandleError(defaultError, data); return new CoreCustomURLSchemesHandleError(defaultError, data);
} }
@ -399,11 +399,11 @@ export class CoreCustomURLSchemesProvider {
// Error decoding the parameter. // Error decoding the parameter.
this.logger.error('Error decoding parameter received for login SSO'); this.logger.error('Error decoding parameter received for login SSO');
throw new CoreCustomURLSchemesHandleError(new CoreError(Translate.instant('core.login.invalidsite'), { throw new CoreCustomURLSchemesHandleError(new CoreError(Translate.instant('core.login.invalidsite'), { debug: {
code: 'errordecodingparameter', code: 'errordecodingparameter',
details: `Error when trying to decode base 64 string.<br><br>URL: ${originalUrl}<br><br>Text to decode: ${url}` + details: `Error when trying to decode base 64 string.<br><br>URL: ${originalUrl}<br><br>Text to decode: ${url}` +
`<br><br>Error: ${CoreErrorHelper.getErrorMessageFromError(err)}`, `<br><br>Error: ${CoreErrorHelper.getErrorMessageFromError(err)}`,
})); } }));
} }
const data: CoreCustomURLSchemesParams = await CoreLoginHelper.validateBrowserSSOLogin(url); const data: CoreCustomURLSchemesParams = await CoreLoginHelper.validateBrowserSSOLogin(url);
@ -529,10 +529,10 @@ export class CoreCustomURLSchemesProvider {
CoreLoginHelper.treatUserTokenError(error.data.siteUrl, <CoreWSError> error.error); CoreLoginHelper.treatUserTokenError(error.data.siteUrl, <CoreWSError> error.error);
CoreSites.logout(); CoreSites.logout();
} else { } else {
CoreDomUtils.showErrorModal(error.error ?? new CoreError(Translate.instant('core.login.invalidsite'), { CoreDomUtils.showErrorModal(error.error ?? new CoreError(Translate.instant('core.login.invalidsite'), { debug: {
code: 'unknownerror', code: 'unknownerror',
details: 'Unknown error when treating a URL scheme.', details: 'Unknown error when treating a URL scheme.',
})); } }));
} }
} }