Skip to content

Commit

Permalink
--wip-- [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
freaz committed Feb 20, 2023
1 parent cb7fcd3 commit ce9709d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 53 deletions.
43 changes: 11 additions & 32 deletions src/core/errors/fetch.errors.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,27 @@
import { ErrorBase } from './errors';

interface NetworkError {
kind: 'network';
issue: 'unsigned-ssl' | 'dns' | 'timeout' | 'reject';
}

interface RequestError {
kind: 'request';
issue: 'abort' | 'timeout';
}
type NetworkErrorIssue = 'unsigned-ssl' | 'dns' | 'timeout' | 'reject';
type RequestErrorIssue = 'abort' | 'timeout';
type FetchErrorIssue = NetworkErrorIssue | RequestErrorIssue;

export type FetchErrorIssue = NetworkError['issue'] | RequestError['issue'];

export class FetchErrorBase extends ErrorBase {
export abstract class FetchError extends ErrorBase {
constructor(kind: string, public issue: FetchErrorIssue) {
super(kind, `Fetch failed: ${issue} issue`);
}
}

export class NetworkFetchError extends FetchErrorBase {
constructor(public override issue: NetworkError['issue']) {
super('NetworkError', issue);
}

public get normalized(): NetworkError {
return { kind: 'network', issue: this.issue };
export class NetworkFetchError extends FetchError {
constructor(public override issue: NetworkErrorIssue) {
super(NetworkFetchError.name, issue);
}
}

export class RequestFetchError extends FetchErrorBase {
constructor(public override issue: RequestError['issue']) {
super('RequestError', issue);
}

public get normalized(): RequestError {
return { kind: 'request', issue: this.issue };
export class RequestFetchError extends FetchError {
constructor(public override issue: RequestErrorIssue) {
super(RequestFetchError.name, issue);
}
}

export type FetchError = NetworkFetchError | RequestFetchError;

export function isFetchError(input: unknown): input is FetchError {
return (
typeof input === 'object' &&
(input instanceof NetworkFetchError || input instanceof RequestFetchError)
);
return input instanceof FetchError;
}
20 changes: 7 additions & 13 deletions src/core/errors/filesystem.errors.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
import { ErrorBase } from './errors';

export class FileExistsError extends ErrorBase {
export abstract class FileSystemError extends ErrorBase {}

export class FileExistsError extends FileSystemError {
constructor(message: string) {
super(FileExistsError.name, message);
}
}

export class PermissionDeniedError extends ErrorBase {
export class PermissionDeniedError extends FileSystemError {
constructor(message: string) {
super(PermissionDeniedError.name, message);
}
}

export class NotEmptyError extends ErrorBase {
export class NotEmptyError extends FileSystemError {
constructor(message: string) {
super(NotEmptyError.name, message);
}
}

export class NotFoundError extends ErrorBase {
export class NotFoundError extends FileSystemError {
constructor(message: string) {
super(NotFoundError.name, message);
}
}

export class UnknownFileSystemError extends ErrorBase {
export class UnknownFileSystemError extends FileSystemError {
constructor(message: string) {
super(UnknownFileSystemError.name, message);
}
}

// TODO: Turn to class FileSystemError
export type FileSystemError =
| FileExistsError
| PermissionDeniedError
| NotEmptyError
| NotFoundError
| UnknownFileSystemError;
2 changes: 1 addition & 1 deletion src/core/events/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type Interceptable = {
events?: Events;
};

type EventContextBase = {
export type EventContextBase = {
readonly time: Date;
readonly usecase?: string;
readonly profile?: string;
Expand Down
40 changes: 33 additions & 7 deletions src/core/events/failure/event-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import type { ILogger, ITimers } from '../../../interfaces';
import { clone } from '../../../lib';
import { isFetchError, SDKBindError, UnexpectedError } from '../../errors';
import type { Events } from '../events';
import type { FetchError } from '../../errors';
import {
isFetchError,
NetworkFetchError,
RequestFetchError,
SDKBindError,
UnexpectedError,
} from '../../errors';
import type { EventContextBase, Events } from '../events';
import type { FailurePolicyRouter } from './policies';
import type { ExecutionFailure, FailurePolicyReason } from './policy';
import type {
Expand Down Expand Up @@ -363,11 +370,7 @@ function registerNetworkHooks(
let failure: ExecutionFailure;

if (isFetchError(error)) {
failure = {
time: context.time.getTime(),
registryCacheAge: 0, // TODO,
...error.normalized,
};
failure = fetchErrorToFailure(context, error);
} else {
failure = {
kind: 'unknown',
Expand Down Expand Up @@ -445,3 +448,26 @@ function registerNetworkHooks(
}
});
}

function fetchErrorToFailure(
context: EventContextBase,
err: FetchError
): ExecutionFailure {
if (err instanceof NetworkFetchError) {
return {
time: context.time.getTime(),
registryCacheAge: 0, // TODO,
kind: 'network',
issue: err.issue,
};
} else if (err instanceof RequestFetchError) {
return {
time: context.time.getTime(),
registryCacheAge: 0, // TODO,
kind: 'request',
issue: err.issue,
};
} else {
throw 'cant reach';
}
}

0 comments on commit ce9709d

Please sign in to comment.