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

View File

@ -31,6 +31,7 @@
"@types/jest": "26.0.22",
"@types/morgan": "1.9.2",
"@types/node": "14.14.37",
"@types/ramda": "0.27.40",
"@types/supertest": "2.0.11",
"@types/ws": "7.4.1",
"@typescript-eslint/eslint-plugin": "4.21.0",

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()
}

View File

@ -773,6 +773,13 @@
resolved "https://registry.yarnpkg.com/@types/qs/-/qs-6.9.6.tgz#df9c3c8b31a247ec315e6996566be3171df4b3b1"
integrity sha512-0/HnwIfW4ki2D8L8c9GVcG5I72s9jP5GSLVF0VIXDW00kmIpA6O33G7a8n59Tmh7Nz0WUC3rSb7PTY/sdW2JzA==
"@types/ramda@0.27.40":
version "0.27.40"
resolved "https://registry.yarnpkg.com/@types/ramda/-/ramda-0.27.40.tgz#99f307356fe553095ee4d3c2af2b0eb3af7a8413"
integrity sha512-V99ZfTH2tqVYdLDAlgh2uT+N074HPgqnAsMjALKSBqogYd0HbuuGMqNukJ6fk9Ml/Htaus76fsc4Yh3p7q1VdQ==
dependencies:
ts-toolbelt "^6.15.1"
"@types/range-parser@*":
version "1.2.3"
resolved "https://registry.yarnpkg.com/@types/range-parser/-/range-parser-1.2.3.tgz#7ee330ba7caafb98090bece86a5ee44115904c2c"
@ -5921,6 +5928,11 @@ ts-node@9.1.1, ts-node@^9.0.0:
source-map-support "^0.5.17"
yn "3.1.1"
ts-toolbelt@^6.15.1:
version "6.15.5"
resolved "https://registry.yarnpkg.com/ts-toolbelt/-/ts-toolbelt-6.15.5.tgz#cb3b43ed725cb63644782c64fbcad7d8f28c0a83"
integrity sha512-FZIXf1ksVyLcfr7M317jbB67XFJhOO1YqdTcuGaq9q5jLUoTikukZ+98TPjKiP2jC5CgmYdWWYs0s2nLSU0/1A==
tsconfig-paths@^3.9.0:
version "3.9.0"
resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.9.0.tgz#098547a6c4448807e8fcb8eae081064ee9a3c90b"