mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-13 22:33:03 -05:00
feat(callstatelib): add a callstate publishable lib
This commit is contained in:
55
libs/shared/ngrx-callstate-store/src/lib/call-state.model.ts
Normal file
55
libs/shared/ngrx-callstate-store/src/lib/call-state.model.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { CustomError, ErrorHandler } from './external.model';
|
||||
|
||||
export type LoadingState = 'INIT' | 'LOADING' | 'LOADED';
|
||||
|
||||
export const UNKNOWN_ERROR_CAUSE = 'UNKNOWN_ERROR';
|
||||
export const UNKNOWN_ERROR_MESSAGE = 'unknown error occured';
|
||||
|
||||
export class CallStateError {
|
||||
name: string;
|
||||
message: string;
|
||||
stack?: string;
|
||||
|
||||
constructor(name = '', message = '') {
|
||||
this.name = name;
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
|
||||
export class CallStateErrorHandler implements ErrorHandler<CallStateError> {
|
||||
toError = (error: unknown): CallStateError => {
|
||||
if (error instanceof CallStateError) return error;
|
||||
if (error instanceof Error)
|
||||
return new CallStateError(error.name, error.message);
|
||||
return new CallStateError(UNKNOWN_ERROR_CAUSE, UNKNOWN_ERROR_MESSAGE);
|
||||
};
|
||||
|
||||
getErrorMessage = (error?: CallStateError): string | undefined => {
|
||||
return error?.message;
|
||||
};
|
||||
}
|
||||
|
||||
export interface EsuiteError {
|
||||
code: string;
|
||||
}
|
||||
|
||||
export interface ErrorState {
|
||||
error: CustomError;
|
||||
}
|
||||
|
||||
export type CallState = LoadingState | ErrorState;
|
||||
|
||||
export const getErrorCallState = (
|
||||
callState: CallState
|
||||
): CustomError | undefined => {
|
||||
if (isErrorState(callState)) {
|
||||
return callState.error;
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
|
||||
export const isLoadedOrInError = (callState: CallState): boolean =>
|
||||
callState === 'LOADED' || isErrorState(callState);
|
||||
|
||||
export const isErrorState = (callState: CallState): callState is ErrorState =>
|
||||
Object.prototype.hasOwnProperty.call(callState, 'error');
|
||||
Reference in New Issue
Block a user