diff --git a/src/core/classes/errors/error.ts b/src/core/classes/errors/error.ts index 44ed1a7d2..48bfb0d84 100644 --- a/src/core/classes/errors/error.ts +++ b/src/core/classes/errors/error.ts @@ -24,20 +24,29 @@ import { CoreErrorObject } from '@services/error-helper'; */ export class CoreError extends Error { + title?: string; debug?: CoreErrorDebug; - constructor(message?: string, debug?: CoreErrorDebug) { + constructor(message?: string, options: CoreErrorOptions = {}) { super(message); // Fix prototype chain: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#support-for-newtarget this.name = new.target.name; 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. */ diff --git a/src/core/classes/errors/siteerror.ts b/src/core/classes/errors/siteerror.ts index 62dffed76..6bb94712b 100644 --- a/src/core/classes/errors/siteerror.ts +++ b/src/core/classes/errors/siteerror.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // 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'; /** @@ -20,13 +20,11 @@ import { CoreUserSupportConfig } from '@features/user/classes/support/support-co */ export class CoreSiteError extends CoreError { - debug?: CoreErrorDebug; supportConfig?: CoreUserSupportConfig; constructor(options: CoreSiteErrorOptions) { - super(options.message); + super(options.message, { title: options.title, debug: options.debug }); - this.debug = options.debug; this.supportConfig = options.supportConfig; } @@ -43,12 +41,9 @@ export class CoreSiteError extends CoreError { } -export type CoreSiteErrorOptions = { +export type CoreSiteErrorOptions = CoreErrorOptions & { message: string; - // Debugging information. - debug?: CoreErrorDebug; - // Configuration to use to contact site support. If this attribute is present, it means // that the error warrants contacting support. supportConfig?: CoreUserSupportConfig; diff --git a/src/core/services/sites.ts b/src/core/services/sites.ts index b761cc9f6..f3a713209 100644 --- a/src/core/services/sites.ts +++ b/src/core/services/sites.ts @@ -286,10 +286,10 @@ export class CoreSitesProvider { siteUrl = CoreUrl.formatURL(siteUrl); if (!CoreUrl.isHttpURL(siteUrl)) { - throw new CoreError(Translate.instant('core.login.invalidsite'), { + throw new CoreError(Translate.instant('core.login.invalidsite'), { debug: { code: 'invalidprotocol', details: `URL contains an invalid protocol when checking site.

Origin: ${origin}.

URL: ${siteUrl}.`, - }); + } }); } if (!CoreNetwork.isOnline()) { diff --git a/src/core/services/urlschemes.ts b/src/core/services/urlschemes.ts index 833bdcc8e..22d876bf8 100644 --- a/src/core/services/urlschemes.ts +++ b/src/core/services/urlschemes.ts @@ -52,10 +52,10 @@ export class CoreCustomURLSchemesProvider { * @returns Error. */ 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', details: `Error when treating a URL scheme, it seems the URL is not valid.

URL: ${url}`, - }); + } }); return new CoreCustomURLSchemesHandleError(defaultError, data); } @@ -399,11 +399,11 @@ export class CoreCustomURLSchemesProvider { // Error decoding the parameter. 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', details: `Error when trying to decode base 64 string.

URL: ${originalUrl}

Text to decode: ${url}` + `

Error: ${CoreErrorHelper.getErrorMessageFromError(err)}`, - })); + } })); } const data: CoreCustomURLSchemesParams = await CoreLoginHelper.validateBrowserSSOLogin(url); @@ -529,10 +529,10 @@ export class CoreCustomURLSchemesProvider { CoreLoginHelper.treatUserTokenError(error.data.siteUrl, error.error); CoreSites.logout(); } 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', details: 'Unknown error when treating a URL scheme.', - })); + } })); } }