feat: add error handling for duplicate or not exists

This commit is contained in:
wancat committed 2023-08-21 22:22:01 +08:00
1 parent 9b78f0d41c
commit 5a1fc6499a
3 files changed
+72 -42

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
/* eslint-disable max-classes-per-file */
export class DuplicateItemError extends Error {
constructor(message) {
super(message)
this.name = 'DuplicateItemError'
}
}
export class ParentNotExistsError extends Error {
constructor(message) {
super(message)
this.name = 'ParentNotExistsError'
}
}
+9 -6
View File
@@ -1,5 +1,6 @@
import path from 'path' import path from 'path'
import File from './file.js' import File from './file.js'
import { DuplicateItemError, ParentNotExistsError } from './errors.js'
export default class FileNode extends File { export default class FileNode extends File {
constructor(file) { constructor(file) {
@@ -10,7 +11,11 @@ export default class FileNode extends File {
addChild(fileInput) { addChild(fileInput) {
const file = new File(fileInput) const file = new File(fileInput)
if (file.path === this.path) { if (file.path === this.path) {
throw new Error('File already exists in the tree') throw new DuplicateItemError('File already exists in the tree')
}
if (this.children.some((child) => child.path === file.path)) {
throw new DuplicateItemError('File already exists in the tree')
} }
const parentPath = path.dirname(file.path) const parentPath = path.dirname(file.path)
@@ -23,19 +28,17 @@ export default class FileNode extends File {
if (childNode) { if (childNode) {
childNode.addChild(file) childNode.addChild(file)
} else { } else {
console.log(fileInput, this) throw new ParentNotExistsError('Parent path not found in the tree')
throw new Error('Parent path not found in the tree')
} }
} }
} }
toString() { toString() {
const indent = ' '.repeat(this.path.split('/').length - 1) const indent = ' '.repeat(this.path.split('/').length - 1)
const suffix = this.isDirectory() ? '/' : ''
const childrenString = this.children const childrenString = this.children
.map((child) => child.toString()) .map((child) => child.toString())
.join('') .join('')
return `${indent}${this.name}${ return `${indent}${this.name}${suffix}\n${childrenString}`
this.isDirectory() ? '/' : ''
}\n${childrenString}`
} }
} }
+49 -36
View File
@@ -1,31 +1,51 @@
import { DuplicateItemError, ParentNotExistsError } from './errors.js'
import FileNode from './fileNode.js' import FileNode from './fileNode.js'
test('addChild method adds a child node to the tree', () => { describe('FileNode', () => {
const files = [ let rootNode
{
beforeEach(() => {
rootNode = new FileNode({
path: '/home/rudeus', path: '/home/rudeus',
name: 'rudeus', name: 'rudeus',
type: 'folder', type: 'folder',
discovered: 'test', discovered: true,
}, })
{ })
path: '/home/rudeus/.bash_history',
name: '.bash_history', test('should throw error when adding duplicate item', () => {
type: 'file', const child = {
discovered: 'test', path: '/home/rudeus/test',
}, name: 'test',
{ type: 'folder',
path: '/home/rudeus/.viminfo', }
name: '.viminfo',
type: 'file', // Adding the child for the first time
discovered: 'test', rootNode.addChild(child)
},
{ expect(rootNode.children.length).toBe(1)
path: '/home/rudeus/.zshrc',
name: '.zshrc', // Trying to add the same child again should throw an error
type: 'file', expect(() => {
discovered: 'test', rootNode.addChild(child)
}, }).toThrow(DuplicateItemError)
})
test('should throw error when parent does not exist', () => {
const child = {
path: '/home/rudeus/alphabet/test',
name: 'test',
type: 'folder',
}
// Trying to add to a nonexistent parent should throw an error
expect(() => {
rootNode.addChild(child)
}).toThrow(ParentNotExistsError)
})
test('adds nested children to the tree', () => {
const files = [
{ {
path: '/home/rudeus/forgotten_scroll.txt', path: '/home/rudeus/forgotten_scroll.txt',
name: 'forgotten_scroll.txt', name: 'forgotten_scroll.txt',
@@ -38,12 +58,6 @@ test('addChild method adds a child node to the tree', () => {
type: 'folder', type: 'folder',
discovered: 'test', discovered: 'test',
}, },
{
path: '/home/rudeus/test/.b',
name: '.b',
type: 'file',
discovered: false,
},
{ {
path: '/home/rudeus/test/a', path: '/home/rudeus/test/a',
name: 'a', name: 'a',
@@ -52,13 +66,12 @@ test('addChild method adds a child node to the tree', () => {
}, },
] ]
const root = new FileNode(files[0]) files.forEach((file) => {
rootNode.addChild(file)
files.slice(1).forEach((file) => {
root.addChild(file)
}) })
console.log(root.toString()) expect(rootNode.children.length).toBe(2)
expect(rootNode.children[1].children.length).toBe(1)
expect(root.children.length).toBe(5) console.log(rootNode.toString())
})
}) })