chore(server): added functional flavour to error helpers

This commit is contained in:
Roberto Tonino
2021-04-25 00:03:57 +02:00
parent 2433209676
commit 57987a83d0
5 changed files with 61 additions and 4 deletions

37
server/src/functors/IO.ts Normal file
View File

@@ -0,0 +1,37 @@
import { compose } from 'ramda'
export class IO {
public unsafePerformIO: any
constructor(fn: any) {
this.unsafePerformIO = fn
}
// [util.inspect.custom]() {
// return 'IO(?)';
// }
// ----- Pointed IO
static of(x: any) {
return new IO(() => x)
}
// ----- Functor IO
map(fn: any) {
return new IO(compose(fn, this.unsafePerformIO))
}
// ----- Applicative IO
ap(f: any) {
return this.chain((fn: any) => f.map(fn))
}
// ----- Monad IO
chain(fn: any) {
return this.map(fn).join()
}
join() {
return new IO(() => this.unsafePerformIO().unsafePerformIO())
}
}

View File

@@ -1,5 +1,12 @@
export const logError = (console: any) => (errorText: string) => console.error(`[deemix-server]: ${errorText}`)
export const consoleError = logError(console)
import { compose, concat, map } from 'ramda'
import { IO } from '../functors/IO'
export const consoleErrorIo = IO.of(console.error)
const prependDeemix = concat('[deemix-server]: ')
export const consoleError = (errorText: string) =>
map((fn: any) => compose(fn, prependDeemix)(errorText), consoleErrorIo)
export class BadRequestError extends Error {
constructor() {

View File

@@ -2,7 +2,7 @@ import { RequestHandler } from 'express'
import { ApiHandler } from '../../../types'
import { dz } from '../../../main'
import { isObjectEmpy } from '../../../helpers/primitive-checks'
import { BadRequestError, consoleError, isBadRequestError } from '../../../helpers/errors'
import { BadRequestError, isBadRequestError, consoleError } from '../../../helpers/errors'
export interface RawChartTracksQuery {
id: string
@@ -27,7 +27,7 @@ const handler: RequestHandler<{}, {}, {}, RawChartTracksQuery> = async (req, res
next()
} catch (error) {
if (isBadRequestError(error)) {
consoleError(error.message)
consoleError(error.message).unsafePerformIO()
res.status(400).send()
return next()
}