diff --git a/src/app/classes/errors/ajaxerror.ts b/src/app/classes/errors/ajaxerror.ts new file mode 100644 index 000000000..7cf6b1363 --- /dev/null +++ b/src/app/classes/errors/ajaxerror.ts @@ -0,0 +1,30 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { CoreError } from '@classes/errors/error'; + +/** + * Generic error returned by an Ajax call. + */ +export class CoreAjaxError extends CoreError { + + available?: number; // Whether the AJAX call is available. 0 if unknown, 1 if available, -1 if not available. + + constructor(message: string, available?: number) { + super(message); + + this.available = typeof available == 'undefined' ? 0 : available; + } + +} diff --git a/src/app/classes/errors/ajaxwserror.ts b/src/app/classes/errors/ajaxwserror.ts new file mode 100644 index 000000000..c2e5df5c3 --- /dev/null +++ b/src/app/classes/errors/ajaxwserror.ts @@ -0,0 +1,53 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { CoreError } from '@classes/errors/error'; + +/** + * Error returned by WS. + */ +export class CoreAjaxWSError extends CoreError { + + exception?: string; // Name of the Moodle exception. + errorcode?: string; + warningcode?: string; + link?: string; // Link to the site. + moreinfourl?: string; // Link to a page with more info. + debuginfo?: string; // Debug info. Only if debug mode is enabled. + backtrace?: string; // Backtrace. Only if debug mode is enabled. + available?: number; // Whether the AJAX call is available. 0 if unknown, 1 if available, -1 if not available. + + // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any + constructor(error: any, available?: number) { + super(error.message); + + this.exception = error.exception; + this.errorcode = error.errorcode; + this.warningcode = error.warningcode; + this.link = error.link; + this.moreinfourl = error.moreinfourl; + this.debuginfo = error.debuginfo; + this.backtrace = error.backtrace; + + this.available = available; + if (typeof this.available == 'undefined') { + if (this.errorcode) { + this.available = this.errorcode == 'invalidrecord' ? -1 : 1; + } else { + this.available = 0; + } + } + } + +} diff --git a/src/app/classes/errors/cancelederror.ts b/src/app/classes/errors/cancelederror.ts new file mode 100644 index 000000000..1be8af272 --- /dev/null +++ b/src/app/classes/errors/cancelederror.ts @@ -0,0 +1,20 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { CoreSilentError } from '@classes/errors/silenterror'; + +/** + * User canceled an action. + */ +export class CoreCanceledError extends CoreSilentError { } diff --git a/src/app/classes/error.ts b/src/app/classes/errors/error.ts similarity index 100% rename from src/app/classes/error.ts rename to src/app/classes/errors/error.ts diff --git a/src/app/classes/errors/silenterror.ts b/src/app/classes/errors/silenterror.ts new file mode 100644 index 000000000..13a72abcc --- /dev/null +++ b/src/app/classes/errors/silenterror.ts @@ -0,0 +1,20 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { CoreError } from '@classes/errors/error'; + +/** + * Error that won't be displayed to the user. + */ +export class CoreSilentError extends CoreError { } diff --git a/src/app/classes/errors/siteerror.ts b/src/app/classes/errors/siteerror.ts new file mode 100644 index 000000000..a459a0257 --- /dev/null +++ b/src/app/classes/errors/siteerror.ts @@ -0,0 +1,41 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { CoreError } from '@classes/errors/error'; + +/** + * Error returned when performing operations regarding a site (check if it exists, authenticate user, etc.). + */ +export class CoreSiteError extends CoreError { + + errorcode?: string; + critical?: boolean; + loggedOut?: boolean; + + constructor(protected error: SiteError) { + super(error.message); + + this.errorcode = error.errorcode; + this.critical = error.critical; + this.loggedOut = error.loggedOut; + } + +} + +export type SiteError = { + message: string; + errorcode?: string; + critical?: boolean; // Whether the error is important enough to abort the operation. + loggedOut?: boolean; // Whether site has been marked as logged out. +}; diff --git a/src/app/classes/errors/wserror.ts b/src/app/classes/errors/wserror.ts new file mode 100644 index 000000000..27e3df43f --- /dev/null +++ b/src/app/classes/errors/wserror.ts @@ -0,0 +1,42 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { CoreError } from '@classes/errors/error'; + +/** + * Error returned by WS. + */ +export class CoreWSError extends CoreError { + + exception?: string; // Name of the Moodle exception. + errorcode?: string; + warningcode?: string; + link?: string; // Link to the site. + moreinfourl?: string; // Link to a page with more info. + debuginfo?: string; // Debug info. Only if debug mode is enabled. + backtrace?: string; // Backtrace. Only if debug mode is enabled. + + constructor(error: any) { + super(error.message); + + this.exception = error.exception; + this.errorcode = error.errorcode; + this.warningcode = error.warningcode; + this.link = error.link; + this.moreinfourl = error.moreinfourl; + this.debuginfo = error.debuginfo; + this.backtrace = error.backtrace; + } + +} diff --git a/src/app/services/geolocation.ts b/src/app/services/geolocation.ts index 7fe1db82a..0423c0cfc 100644 --- a/src/app/services/geolocation.ts +++ b/src/app/services/geolocation.ts @@ -16,7 +16,7 @@ import { Injectable } from '@angular/core'; import { Coordinates } from '@ionic-native/geolocation'; import { CoreApp } from '@services/app'; -import { CoreError } from '@classes/error'; +import { CoreError } from '@classes/errors/error'; import { Geolocation, Diagnostic, makeSingleton } from '@singletons/core.singletons'; @Injectable() diff --git a/src/app/services/utils/text.ts b/src/app/services/utils/text.ts index 30ce8fb26..86a7c3fb0 100644 --- a/src/app/services/utils/text.ts +++ b/src/app/services/utils/text.ts @@ -17,6 +17,7 @@ import { DomSanitizer, SafeUrl } from '@angular/platform-browser'; import { CoreApp } from '@services/app'; import { CoreLang } from '@services/lang'; +import { CoreError } from '@classes/errors/error'; import { makeSingleton, Translate } from '@singletons/core.singletons'; import { CoreWSExternalFile } from '@services/ws'; import { Locutus } from '@singletons/locutus'; @@ -29,6 +30,8 @@ export type CoreTextErrorObject = { error?: string; content?: string; body?: string; + debuginfo?: string; + backtrace?: string; }; /* @@ -526,10 +529,13 @@ export class CoreTextUtilsProvider { * @param error Error object. * @return Error message, undefined if not found. */ - getErrorMessageFromError(error: string | CoreTextErrorObject): string { + getErrorMessageFromError(error: string | CoreError | CoreTextErrorObject): string { if (typeof error == 'string') { return error; } + if (error instanceof CoreError) { + return error.message; + } return error && (error.message || error.error || error.content || error.body); } diff --git a/src/app/services/utils/utils.ts b/src/app/services/utils/utils.ts index c61f367b4..83df2f679 100644 --- a/src/app/services/utils/utils.ts +++ b/src/app/services/utils/utils.ts @@ -21,10 +21,11 @@ import { CoreApp } from '@services/app'; import { CoreEvents, CoreEventsProvider } from '@services/events'; import { CoreFile } from '@services/file'; import { CoreLang } from '@services/lang'; -import { CoreWS, CoreWSError, CoreWSExternalFile } from '@services/ws'; +import { CoreWS, CoreWSExternalFile } from '@services/ws'; import { CoreDomUtils } from '@services/utils/dom'; import { CoreMimetypeUtils } from '@services/utils/mimetype'; import { CoreTextUtils } from '@services/utils/text'; +import { CoreWSError } from '@classes/errors/wserror'; import { makeSingleton, Clipboard, InAppBrowser, Platform, FileOpener, WebIntent, QRScanner, Translate, } from '@singletons/core.singletons'; @@ -325,6 +326,7 @@ export class CoreUtilsProvider { * @param message The message to include in the error. * @param needsTranslate If the message needs to be translated. * @return Fake WS error. + * @deprecated since 3.9.5. Just create the error directly. */ createFakeWSError(message: string, needsTranslate?: boolean): CoreWSError { return CoreWS.instance.createFakeWSError(message, needsTranslate);