Skip to content

Commit

Permalink
refactor: error handling by extracting messages
Browse files Browse the repository at this point in the history
  • Loading branch information
DIY0R committed Sep 5, 2024
1 parent 957dddd commit 673fd4d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 27 deletions.
49 changes: 27 additions & 22 deletions lib/graph/file.graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
uuidType,
} from '../interfaces';
import { StorageFile } from './storage.file';
import { AsyncTaskQueue, uuid } from '../utils';
import { AsyncTaskQueue, uuid, createError } from '../utils';

class FileGraphIml implements FileGraphAbstract {
constructor(
Expand Down Expand Up @@ -93,8 +93,9 @@ class FileGraphIml implements FileGraphAbstract {
const index = ids.indexOf(vertex.id);
if (index === -1) return;
const nextVertexId = ids[index + 1];
if (nextVertexId && !vertex.links.includes(nextVertexId))
if (nextVertexId && !vertex.links.includes(nextVertexId)) {
return { links: [...vertex.links, nextVertexId] };
}
};

const updateResult = await this.storageFile.updateLine(updater);
Expand All @@ -107,12 +108,14 @@ class FileGraphIml implements FileGraphAbstract {
): Promise<boolean> {
return this.updateArc(targetVertexId, vertex => {
if (vertex.id !== sourceVertexId) return;
if (!vertex.links.includes(targetVertexId))
return { links: [...vertex.links, targetVertexId] };

throw new Error(
`targetVertexId: ${targetVertexId} already exists in vertex ${sourceVertexId}`,
);
if (vertex.links.includes(targetVertexId)) {
throw createError(
'TARGET_VERTEX_ALREADY_EXISTS',
targetVertexId,
sourceVertexId,
);
}
return { links: [...vertex.links, targetVertexId] };
});
}

Expand All @@ -122,12 +125,14 @@ class FileGraphIml implements FileGraphAbstract {
): Promise<boolean> {
return this.updateArc(targetVertexId, vertex => {
if (vertex.id !== sourceVertexId) return;
if (vertex.links.includes(targetVertexId))
return { links: vertex.links.filter(v => v !== targetVertexId) };

throw new Error(
`targetVertexId: ${targetVertexId} don't exists in vertex ${sourceVertexId}`,
);
if (!vertex.links.includes(targetVertexId)) {
throw createError(
'TARGET_VERTEX_DOES_NOT_EXIST',
targetVertexId,
sourceVertexId,
);
}
return { links: vertex.links.filter(v => v !== targetVertexId) };
});
}

Expand All @@ -138,8 +143,9 @@ class FileGraphIml implements FileGraphAbstract {
const targetVertexExists = await this.findOne(
vertex => vertex.id === sourceVertexId,
);
if (!targetVertexExists)
throw new Error(`Target vertex with ID "${targetVertexId}" not found`);
if (!targetVertexExists) {
throw createError('TARGET_VERTEX_NOT_FOUND', targetVertexId);
}

return targetVertexExists.links.includes(targetVertexId);
}
Expand All @@ -148,13 +154,11 @@ class FileGraphIml implements FileGraphAbstract {
vertexId: uuidType,
maxLevel: number,
): Promise<IVertexTree<T>[]> {
if (maxLevel < 0) throw new Error('Level must be a non-negative integer.');
if (maxLevel < 0) throw createError('NEGATIVE_LEVEL');
const startingVertex = await this.findOne<T>(
vertex => vertex.id === vertexId,
);

if (!startingVertex)
throw new Error(`Vertex with id ${vertexId} not found.`);
if (!startingVertex) throw createError('VERTEX_NOT_FOUND', vertexId);

const resultVertices: IVertexTree<T>[] = [];
const queue = [{ vertex: startingVertex, currentLevel: 0 }];
Expand Down Expand Up @@ -187,8 +191,9 @@ class FileGraphIml implements FileGraphAbstract {
const targetVertexExists = await this.findOne(
vertex => vertex.id === targetVertexId,
);
if (!targetVertexExists)
throw new Error(`Target vertex with ID "${targetVertexId}" not found`);
if (!targetVertexExists) {
throw createError('TARGET_VERTEX_NOT_FOUND', targetVertexId);
}

const updateResult = await this.storageFile.updateLine(updater);
return updateResult;
Expand Down
25 changes: 25 additions & 0 deletions lib/utils/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { uuidType } from '../interfaces';

const ERRORS = {
VERTEX_NOT_FOUND: (id: uuidType) => `Vertex with id ${id} not found.`,
TARGET_VERTEX_NOT_FOUND: (id: uuidType) =>
`Target vertex with ID "${id}" not found.`,
TARGET_VERTEX_DOES_NOT_EXIST: (targetId: uuidType, sourceId: uuidType) =>
`targetVertexId: ${targetId} don't exist in vertex ${sourceId}`,
TARGET_VERTEX_ALREADY_EXISTS: (targetId: uuidType, sourceId: uuidType) =>
`targetVertexId: ${targetId} already exists in vertex ${sourceId}`,
NEGATIVE_LEVEL: 'Level must be a non-negative integer.',
};

const createError = (type: keyof typeof ERRORS, ...args: any[]) => {
const errorTemplate: string | ((...args: any[]) => string) = ERRORS[type];
if (!errorTemplate) throw new Error('Unknown error type');

return new Error(
typeof errorTemplate === 'function'
? errorTemplate(...args)
: errorTemplate,
);
};

export { createError };
1 change: 1 addition & 0 deletions lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './uuid';
export * from './async-task-queue';
export * from './merge-vertex';
export * from './errors';
8 changes: 3 additions & 5 deletions test/graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assert from 'node:assert';
import { before, describe, it } from 'node:test';
import { FileGraph, IUuidArray, uuidType } from 'lib';
import { writeFileSync } from 'node:fs';
import { createError } from 'lib/utils';

const pathGraph = 'data.txt';
const graph = FileGraph(pathGraph);
Expand Down Expand Up @@ -163,10 +164,7 @@ describe('Links operations', () => {
await graph.findUpToLevel('A' as uuidType, -1);
assert.fail('Expected error not thrown');
} catch (error) {
assert.strictEqual(
error.message,
'Level must be a non-negative integer.',
);
assert.strictEqual(error.message, createError('NEGATIVE_LEVEL').message);
}
});

Expand All @@ -177,7 +175,7 @@ describe('Links operations', () => {
} catch (error) {
assert.strictEqual(
error.message,
'Vertex with id NonExistentVertex not found.',
createError('VERTEX_NOT_FOUND', 'NonExistentVertex').message,
);
}
});
Expand Down

0 comments on commit 673fd4d

Please sign in to comment.