Skip to content

Commit

Permalink
fix(core): pass specific status to HyperErr on known errors and bubbl…
Browse files Browse the repository at this point in the history
…e as non fatal #621
  • Loading branch information
TillaTheHun0 committed Apr 2, 2024
1 parent bcba779 commit 76f4d42
Show file tree
Hide file tree
Showing 10 changed files with 455 additions and 196 deletions.
420 changes: 330 additions & 90 deletions packages/core/deno.lock

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions packages/core/lib/cache/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function checkKeyIsValid<T extends { key: string }>(input: T) {
* or any of these characters - _ $ +
*/
return /^[a-z]+$/.test(key[0]) && /^[a-z0-9-~_/$/+]+$/.test(key)
}, 'key is not valid')(input)
}, { status: 422, msg: 'key is not valid' })(input)
}

/**
Expand All @@ -52,12 +52,12 @@ export const create = (
ttl?: string,
) =>
of({ store, key, value, ttl })
.map(convertTTL)
.map(removeTTL)
.chain(checkKeyIsValid)
.chain((input) =>
ask(({ svc }: ReaderEnvironment<CachePort>) => {
return Async.of(input)
.map(convertTTL)
.map(removeTTL)
.chain(checkKeyIsValid)
.chain(Async.fromPromise((input) => svc.createDoc(input)))
.bichain($resolveHyperErr, $logHyperErr)
}).chain(lift)
Expand All @@ -70,10 +70,10 @@ export const create = (
*/
export const get = (store: string, key: string) =>
of({ store, key })
.chain(checkKeyIsValid)
.chain((input) =>
ask(({ svc }: ReaderEnvironment<CachePort>) => {
return Async.of(input)
.chain(checkKeyIsValid)
.chain(Async.fromPromise((input) => svc.getDoc(input)))
.bichain($resolveHyperErr, $logHyperErr)
}).chain(lift)
Expand All @@ -94,12 +94,12 @@ export const update = (
ttl?: string,
) =>
of({ store, key, value, ttl })
.map(convertTTL)
.map(removeTTL)
.chain(checkKeyIsValid)
.chain((input) =>
ask(({ svc }: ReaderEnvironment<CachePort>) => {
return Async.of(input)
.map(convertTTL)
.map(removeTTL)
.chain(checkKeyIsValid)
.chain(Async.fromPromise((input) => svc.updateDoc(input)))
.bichain($resolveHyperErr, $logHyperErr)
}).chain(lift)
Expand All @@ -112,10 +112,10 @@ export const update = (
*/
export const del = (store: string, key: string) =>
of({ store, key })
.chain(checkKeyIsValid)
.chain((input) =>
ask(({ svc }: ReaderEnvironment<CachePort>) => {
return Async.of(input)
.chain(checkKeyIsValid)
.chain(Async.fromPromise((input) => svc.deleteDoc(input)))
.bichain($resolveHyperErr, $logHyperErr)
}).chain(lift)
Expand Down
87 changes: 51 additions & 36 deletions packages/core/lib/cache/mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,37 +62,41 @@ Deno.test('cache', async (t) => {
},
)

await t.step('should reject if the name is invalid', async (t) => {
await t.step('should resolve HyperErr if the name is invalid', async (t) => {
await t.step('does not start with alphanumeric', async () => {
await cache
.createStore('_foo')
.toPromise()
.then(() => assert(false))
.catch(() => assert(true))
.then((err: any) => {
assertEquals(err.status, 422)
})
})

await t.step('contains a space', async () => {
await cache
.createStore('foo bar')
.toPromise()
.then(() => assert(false))
.catch(() => assert(true))
.then((err: any) => {
assertEquals(err.status, 422)
})
})

await t.step('contains a slash', async () => {
await cache
.createStore('foo/bar')
.toPromise()
.then(() => assert(false))
.catch(() => assert(true))
.then((err: any) => {
assertEquals(err.status, 422)
})
})

await t.step('contains non URI friendly character', async () => {
await cache
.createStore('foo?bar')
.toPromise()
.then(() => assert(false))
.catch(() => assert(true))
.then((err: any) => {
assertEquals(err.status, 422)
})
})
})
})
Expand All @@ -108,37 +112,41 @@ Deno.test('cache', async (t) => {
.toPromise()
})

await t.step('should reject if the name is invalid', async (t) => {
await t.step('should resolve HyperErr if the name is invalid', async (t) => {
await t.step('does not start with alphanumeric', async () => {
await cache
.deleteStore('_foo')
.toPromise()
.then(() => assert(false))
.catch(() => assert(true))
.then((err: any) => {
assertEquals(err.status, 422)
})
})

await t.step('contains a space', async () => {
await cache
.deleteStore('foo bar')
.toPromise()
.then(() => assert(false))
.catch(() => assert(true))
.then((err: any) => {
assertEquals(err.status, 422)
})
})

await t.step('contains a slash', async () => {
await cache
.deleteStore('foo/bar')
.toPromise()
.then(() => assert(false))
.catch(() => assert(true))
.then((err: any) => {
assertEquals(err.status, 422)
})
})

await t.step('contains non URI friendly character', async () => {
await cache
.deleteStore('foo?bar')
.toPromise()
.then(() => assert(false))
.catch(() => assert(true))
.then((err: any) => {
assertEquals(err.status, 422)
})
})
})
})
Expand All @@ -156,37 +164,41 @@ Deno.test('cache', async (t) => {
.toPromise()
})

await t.step('should reject if the name is invalid', async (t) => {
await t.step('should resolve HyperErr if the name is invalid', async (t) => {
await t.step('does not start with alphanumeric', async () => {
await cache
.queryStore('_foo', 'foo*')
.toPromise()
.then(() => assert(false))
.catch(() => assert(true))
.then((err: any) => {
assertEquals(err.status, 422)
})
})

await t.step('contains a space', async () => {
await cache
.queryStore('foo bar', 'foo*')
.toPromise()
.then(() => assert(false))
.catch(() => assert(true))
.then((err: any) => {
assertEquals(err.status, 422)
})
})

await t.step('contains a slash', async () => {
await cache
.queryStore('foo/bar', 'foo*')
.toPromise()
.then(() => assert(false))
.catch(() => assert(true))
.then((err: any) => {
assertEquals(err.status, 422)
})
})

await t.step('contains non URI friendly character', async () => {
await cache
.queryStore('foo?bar', 'foo*')
.toPromise()
.then(() => assert(false))
.catch(() => assert(true))
.then((err: any) => {
assertEquals(err.status, 422)
})
})
})
})
Expand Down Expand Up @@ -228,12 +240,13 @@ Deno.test('cache', async (t) => {
.toPromise()
})

await t.step('should reject if cache doc has an invalid key', async () => {
await t.step('should resolve HyperErr if cache doc has an invalid key', async () => {
await cache
.createDoc('store', 'Not_Valid', { beep: 'boop' })
.toPromise()
.then(() => assert(false))
.catch(() => assert(true))
.then((err: any) => {
assertEquals(err.status, 422)
})
})
})

Expand Down Expand Up @@ -363,12 +376,13 @@ Deno.test('cache', async (t) => {
.toPromise()
})

await t.step('should reject if cache doc has an invalid key', async () => {
await t.step('should resolve HyperErr if cache doc has an invalid key', async () => {
await cache
.updateDoc('store', 'Not_Valid', { beep: 'boop' })
.toPromise()
.then(() => assert(false))
.catch(() => assert(true))
.then((err: any) => {
assertEquals(err.status, 422)
})
})
})

Expand All @@ -385,12 +399,13 @@ Deno.test('cache', async (t) => {
.toPromise()
})

await t.step('should reject if cache doc has an invalid key', async () => {
await t.step('should resolve HyperErr if cache doc has an invalid key', async () => {
await cache
.deleteDoc('store', 'Not_Valid')
.toPromise()
.then(() => assert(false))
.catch(() => assert(true))
.then((err: any) => {
assertEquals(err.status, 422)
})
})
})
})
12 changes: 6 additions & 6 deletions packages/core/lib/cache/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const checkNameIsValid = is((name: string) => {
// cache names should only start with alphanumeric characters
// should return a true or false
return /^[a-z0-9]+$/.test(name[0]) && /^[a-z0-9-~_]+$/.test(name)
}, 'name is not valid')
}, { status: 422, msg: 'name is not valid' })

export const index = () =>
ask(({ svc }: ReaderEnvironment<CachePort>) => {
Expand All @@ -37,10 +37,10 @@ export const index = () =>
export const create = (name: string) =>
of(name)
.map(toLower)
.chain(checkNameIsValid)
.chain((input) =>
ask(({ svc }: ReaderEnvironment<CachePort>) => {
return Async.of(input)
.chain(checkNameIsValid)
.chain(Async.fromPromise((input) => svc.createStore(input)))
.bichain($resolveHyperErr, $logHyperErr)
}).chain(lift)
Expand All @@ -52,10 +52,10 @@ export const create = (name: string) =>
*/
export const del = (name: string) =>
of(name)
.chain(checkNameIsValid)
.chain((input) =>
ask(({ svc }: ReaderEnvironment<CachePort>) => {
return Async.of(input)
.chain(checkNameIsValid)
.chain(Async.fromPromise((input) => svc.destroyStore(input)))
.bichain($resolveHyperErr, $logHyperErr)
}).chain(lift)
Expand All @@ -67,12 +67,12 @@ export const del = (name: string) =>
* @param {string} pattern
*/
export const query = (name: string, pattern: string) =>
of(name)
.chain(checkNameIsValid)
.map((name) => ({ store: name, pattern }))
of({ store: name, pattern })
.chain((input) =>
ask(({ svc }: ReaderEnvironment<CachePort>) => {
return Async.of(input)
.chain(({ store }) => checkNameIsValid(store))
.map(() => input)
.chain(Async.fromPromise((input) => svc.listDocs(input)))
.bichain($resolveHyperErr, $logHyperErr)
}).chain(lift)
Expand Down
6 changes: 3 additions & 3 deletions packages/core/lib/data/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ const setDescending = <T extends { descending?: boolean }>(arg: T) => ({
descending: Boolean(arg.descending),
})

const checkNameIsValid = is<string>(() => true, 'database name is not valid')
const checkNameIsValid = is<string>(() => true, { status: 422, msg: 'database name is not valid' })

/**
* @param {string} name
*/
export const create = (name: string) =>
of(name)
.chain(checkNameIsValid)
.chain((input) =>
ask(({ svc }: ReaderEnvironment<DataPort>) => {
return Async.of(input)
.chain(checkNameIsValid)
.chain(Async.fromPromise((input) => svc.createDatabase(input)))
.bichain($resolveHyperErr, $logHyperErr)
}).chain(lift)
Expand All @@ -39,10 +39,10 @@ export const create = (name: string) =>
*/
export const remove = (name: string) =>
of(name)
.chain(checkNameIsValid)
.chain((input) =>
ask(({ svc }: ReaderEnvironment<DataPort>) => {
return Async.of(input)
.chain(checkNameIsValid)
.chain(Async.fromPromise((input) => svc.removeDatabase(input)))
.bichain($resolveHyperErr, $logHyperErr)
}).chain(lift)
Expand Down
17 changes: 10 additions & 7 deletions packages/core/lib/queue/mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Deno.test('queue', async (t) => {
},
)

await t.step('should reject if the name is invalid', async (t) => {
await t.step('should resolve HyperErr if the name is invalid', async (t) => {
await t.step('contains a space', async () => {
await queue
.create({
Expand All @@ -86,8 +86,9 @@ Deno.test('queue', async (t) => {
target: 'https://foo.bar',
})
.toPromise()
.then(() => assert(false))
.catch(() => assert(true))
.then((err: any) => {
assertEquals(err.status, 422)
})
})

await t.step('contains a slash', async () => {
Expand All @@ -98,8 +99,9 @@ Deno.test('queue', async (t) => {
target: 'https://foo.bar',
})
.toPromise()
.then(() => assert(false))
.catch(() => assert(true))
.then((err: any) => {
assertEquals(err.status, 422)
})
})

await t.step('contains non URI friendly character', async () => {
Expand All @@ -110,8 +112,9 @@ Deno.test('queue', async (t) => {
target: 'https://foo.bar',
})
.toPromise()
.then(() => assert(false))
.catch(() => assert(true))
.then((err: any) => {
assertEquals(err.status, 422)
})
})
})
})
Expand Down
Loading

0 comments on commit 76f4d42

Please sign in to comment.