From 57987a83d0807d232c10fbdc9c1bdbd417fd4b50 Mon Sep 17 00:00:00 2001 From: Roberto Tonino Date: Sun, 25 Apr 2021 00:03:57 +0200 Subject: [PATCH] chore(server): added functional flavour to error helpers --- server/package.json | 1 + server/src/functors/IO.ts | 37 +++++++++++++++++++++ server/src/helpers/errors.ts | 11 ++++-- server/src/routes/api/get/getChartTracks.ts | 4 +-- server/yarn.lock | 12 +++++++ 5 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 server/src/functors/IO.ts diff --git a/server/package.json b/server/package.json index 486923b..95ea304 100644 --- a/server/package.json +++ b/server/package.json @@ -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", diff --git a/server/src/functors/IO.ts b/server/src/functors/IO.ts new file mode 100644 index 0000000..207bf72 --- /dev/null +++ b/server/src/functors/IO.ts @@ -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()) + } +} diff --git a/server/src/helpers/errors.ts b/server/src/helpers/errors.ts index 758de31..84760bf 100644 --- a/server/src/helpers/errors.ts +++ b/server/src/helpers/errors.ts @@ -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() { diff --git a/server/src/routes/api/get/getChartTracks.ts b/server/src/routes/api/get/getChartTracks.ts index 6c90761..d40bcf8 100644 --- a/server/src/routes/api/get/getChartTracks.ts +++ b/server/src/routes/api/get/getChartTracks.ts @@ -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() } diff --git a/server/yarn.lock b/server/yarn.lock index 1c17069..c1a46dc 100644 --- a/server/yarn.lock +++ b/server/yarn.lock @@ -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"