-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add global errors test: fix login test test: fix login test
- Loading branch information
1 parent
08cda1d
commit 4568cbc
Showing
15 changed files
with
196 additions
and
20 deletions.
There are no files selected for viewing
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
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
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,13 @@ | ||
import { ErrorObject } from '../model/error.model' | ||
|
||
export enum ErrorCode { | ||
USER_NOT_LOGGED_IN, | ||
} | ||
|
||
export const errors: Record<ErrorCode, ErrorObject> = { | ||
[ErrorCode.USER_NOT_LOGGED_IN]: { | ||
code: ErrorCode.USER_NOT_LOGGED_IN, | ||
priority: 2, | ||
message: 'ERROR_USER_NOT_LOGGED_IN', | ||
}, | ||
} |
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,20 @@ | ||
import BackgroundAction from '../../constants/background-actions.enum' | ||
import { Errors } from '../../services/error.service' | ||
import { createMessageHandler } from './message-handler' | ||
|
||
const errors = new Errors() | ||
|
||
export async function getLastError(): Promise<string> { | ||
const errorMessage = await errors.getMostImportantMessage() | ||
|
||
return errorMessage | ||
} | ||
|
||
const messageHandler = createMessageHandler([ | ||
{ | ||
action: BackgroundAction.GET_ERROR, | ||
handler: getLastError, | ||
}, | ||
]) | ||
|
||
export default messageHandler |
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
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
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
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,7 @@ | ||
import { ErrorCode } from '../constants/errors' | ||
|
||
export interface ErrorObject { | ||
code: ErrorCode | ||
priority: number | ||
message: string | ||
} |
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,8 @@ | ||
import { ErrorCode } from '../../constants/errors' | ||
import { ErrorObject } from '../error.model' | ||
|
||
export type Errors = Partial<Record<ErrorCode, ErrorObject>> | ||
|
||
export interface General { | ||
errors: Errors | ||
} |
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,69 @@ | ||
import { ErrorCode, errors as globalErrors } from '../constants/errors' | ||
import { ErrorObject } from '../model/error.model' | ||
import { removeWarningBadge, setWarningBadge } from '../utils/extension' | ||
import { Storage } from './storage/storage.service' | ||
import { Errors as ErrorsModel } from '../model/storage/general.model' | ||
import { Locales } from './locales.service' | ||
|
||
export class Errors { | ||
private storage: Storage = new Storage() | ||
|
||
public async refreshGlobalError() { | ||
const errors = await this.getErrors() | ||
|
||
;(this.hasErrors(errors) ? setWarningBadge : removeWarningBadge)() | ||
} | ||
|
||
public async getMostImportantMessage(): Promise<string | null> { | ||
const errors = await this.getErrors() | ||
|
||
const mostImportantError = Object.values(errors).reduce( | ||
(mostImportantError, currentError) => { | ||
return mostImportantError.priority < currentError.priority ? mostImportantError : currentError | ||
}, | ||
{ priority: 100 } as ErrorObject, | ||
) | ||
|
||
return mostImportantError ? mostImportantError.message : null | ||
} | ||
|
||
public async addGlobalError(errorCode: ErrorCode): Promise<ErrorObject> { | ||
const errors = await this.getErrors() | ||
|
||
const error = { ...globalErrors[errorCode] } | ||
|
||
errors[errorCode] = error | ||
|
||
await this.updateErrors(errors) | ||
|
||
setWarningBadge() | ||
|
||
return error | ||
} | ||
|
||
public async removeGlobalError(errorCode: ErrorCode) { | ||
const errors = await this.getErrors() | ||
|
||
delete errors[errorCode] | ||
|
||
await this.updateErrors(errors) | ||
|
||
if (this.hasErrors(errors)) { | ||
removeWarningBadge() | ||
} | ||
} | ||
|
||
private hasErrors(errors: ErrorsModel): boolean { | ||
return errors && Object.keys(errors).length === 0 | ||
} | ||
|
||
private async getErrors(): Promise<ErrorsModel> { | ||
const { errors } = await this.storage.getGeneral() | ||
|
||
return errors | ||
} | ||
|
||
private updateErrors(errors: ErrorsModel): Promise<void> { | ||
return this.storage.updateGeneral({ errors }) | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.