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