-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added the ErrorHandler module and updated the error approach
- Loading branch information
1 parent
d67817d
commit 41109ff
Showing
11 changed files
with
2,043 additions
and
1,908 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
assets/js/src/core/modules/app/error-handler/classes/api-error.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - Pimcore Open Core License (POCL) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license https://github.com/pimcore/studio-ui-bundle/blob/1.x/LICENSE.md POCL and PCL | ||
*/ | ||
|
||
import { isEmpty, isString } from 'lodash' | ||
import type { FetchBaseQueryError } from '@reduxjs/toolkit/query' | ||
import type { SerializedError } from '@reduxjs/toolkit' | ||
|
||
type ApiErrorData = FetchBaseQueryError | SerializedError | ||
|
||
interface IApiErrorDetails { | ||
detail: string | ||
message: string | ||
} | ||
|
||
const DEFAULT_ERROR_CONTENT = 'Something went wrong.' | ||
|
||
export class ApiError extends Error { | ||
private readonly errorData: ApiErrorData | ||
|
||
constructor (errorData: ApiErrorData) { | ||
super() | ||
|
||
this.errorData = errorData | ||
} | ||
|
||
public getContent (): string { | ||
if (!isEmpty(this.errorData)) { | ||
if ('data' in this.errorData && !isEmpty((this.errorData.data as IApiErrorDetails)?.message)) { | ||
return (this.errorData.data as IApiErrorDetails)?.message | ||
} | ||
|
||
if ('error' in this.errorData && isString(this.errorData.error)) { | ||
return this.errorData.error | ||
} | ||
} | ||
|
||
return DEFAULT_ERROR_CONTENT | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
assets/js/src/core/modules/app/error-handler/classes/general-error.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - Pimcore Open Core License (POCL) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license https://github.com/pimcore/studio-ui-bundle/blob/1.x/LICENSE.md POCL and PCL | ||
*/ | ||
|
||
export class GeneralError extends Error { | ||
private readonly errorMessage: string | ||
|
||
constructor (message: string) { | ||
super() | ||
|
||
this.errorMessage = message | ||
} | ||
|
||
public getContent (): string { | ||
return this.errorMessage | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
assets/js/src/core/modules/app/error-handler/constants/errorTypes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - Pimcore Open Core License (POCL) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license https://github.com/pimcore/studio-ui-bundle/blob/1.x/LICENSE.md POCL and PCL | ||
*/ | ||
|
||
export enum ErrorTypes { | ||
API_ERROR = 'API_ERROR', | ||
GENERAL_ERROR = 'GENERAL_ERROR' | ||
} |
48 changes: 48 additions & 0 deletions
48
assets/js/src/core/modules/app/error-handler/error-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - Pimcore Open Core License (POCL) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license https://github.com/pimcore/studio-ui-bundle/blob/1.x/LICENSE.md POCL and PCL | ||
*/ | ||
|
||
import type { FetchBaseQueryError } from '@reduxjs/toolkit/query' | ||
import type { SerializedError } from '@reduxjs/toolkit' | ||
import { isFunction } from 'lodash' | ||
import { ErrorModalService } from '@Pimcore/modules/app/error-handler/services/error-modal-service' | ||
import { GeneralError } from '@Pimcore/modules/app/error-handler/classes/general-error' | ||
import { ApiError } from '@Pimcore/modules/app/error-handler/classes/api-error' | ||
import { type ErrorTypes } from '@Pimcore/modules/app/error-handler/constants/errorTypes' | ||
|
||
interface ITrackProps { | ||
errorType: keyof typeof ErrorTypes | ||
errorData: FetchBaseQueryError | SerializedError | string | ||
} | ||
|
||
const ERROR_HANDLERS_LIST: Record<ITrackProps['errorType'], (errorData: ITrackProps['errorData']) => void> = { | ||
API_ERROR: (errorData: FetchBaseQueryError | SerializedError) => { | ||
const error = new ApiError(errorData) | ||
|
||
ErrorModalService.showError(error.getContent()) | ||
}, | ||
GENERAL_ERROR: (errorData: string) => { | ||
const error = new GeneralError(errorData) | ||
|
||
ErrorModalService.showError(error.getContent()) | ||
} | ||
} | ||
|
||
export const trackError = ({ errorType, errorData }: ITrackProps): void => { | ||
const errorHandler = ERROR_HANDLERS_LIST[errorType] | ||
|
||
if (isFunction(errorHandler)) { | ||
errorHandler(errorData) | ||
} else { | ||
console.error(`Unhandled error type: ${errorType}`) | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
assets/js/src/core/modules/app/error-handler/services/error-modal-service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - Pimcore Open Core License (POCL) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license https://github.com/pimcore/studio-ui-bundle/blob/1.x/LICENSE.md POCL and PCL | ||
*/ | ||
|
||
import { isEmpty } from 'lodash' | ||
|
||
interface IErrorModalServiceReturn { | ||
setModalInstance: (modal: any) => void | ||
showError: (content: string) => void | ||
} | ||
|
||
export const ErrorModalService = ((): IErrorModalServiceReturn => { | ||
let modalInstance: any = null | ||
|
||
const setModalInstance = (modal: any): void => { | ||
modalInstance = modal | ||
} | ||
|
||
const showError = (content: string): void => { | ||
if (isEmpty(modalInstance)) { | ||
throw new Error('ErrorModalService: Modal instance is not set. Call setModalInstance first.') | ||
} | ||
|
||
modalInstance.error({ content }) | ||
} | ||
|
||
return { | ||
setModalInstance, | ||
showError | ||
} | ||
})() |
14 changes: 0 additions & 14 deletions
14
public/build/a946d68d-b560-4419-9193-270ffd330528/entrypoints.json
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.