Skip to content

Commit

Permalink
Use named status codes
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilkybarkid committed Dec 18, 2023
1 parent 2248a9c commit 444014b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 33 deletions.
56 changes: 31 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@js-temporal/polyfill": "^0.4.4",
"doi-regex": "^0.1.13",
"effect": "^2.0.0-next.58",
"http-status-codes": "^2.3.0",
"ioredis": "^5.3.2"
},
"devDependencies": {
Expand Down
17 changes: 9 additions & 8 deletions src/Router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HttpServer } from '@effect/platform-node'
import { Schema, TreeFormatter } from '@effect/schema'
import { Context, Data, Effect } from 'effect'
import { StatusCodes } from 'http-status-codes'
import * as CoarNotify from './CoarNotify.js'
import * as Doi from './Doi.js'
import * as Redis from './Redis.js'
Expand Down Expand Up @@ -35,13 +36,13 @@ export const Router = HttpServer.router.empty.pipe(
Effect.gen(function* (_) {
yield* _(Effect.logError('Unable to ping Redis').pipe(Effect.annotateLogs({ message: error.message })))

return yield* _(HttpServer.response.json({ status: 'error' }, { status: 503 }))
return yield* _(HttpServer.response.json({ status: 'error' }, { status: StatusCodes.SERVICE_UNAVAILABLE }))
}),
RedisTimeout: error =>
Effect.gen(function* (_) {
yield* _(Effect.logError('Unable to ping Redis').pipe(Effect.annotateLogs({ message: error.message })))

return yield* _(HttpServer.response.json({ status: 'error' }, { status: 503 }))
return yield* _(HttpServer.response.json({ status: 'error' }, { status: StatusCodes.SERVICE_UNAVAILABLE }))
}),
}),
),
Expand Down Expand Up @@ -91,7 +92,7 @@ export const Router = HttpServer.router.empty.pipe(
}),
)

return yield* _(HttpServer.response.empty({ status: 201 }))
return yield* _(HttpServer.response.empty({ status: StatusCodes.CREATED }))
}).pipe(
Effect.catchTags({
ParseError: error =>
Expand All @@ -102,7 +103,7 @@ export const Router = HttpServer.router.empty.pipe(
),
)

return HttpServer.response.empty({ status: 400 })
return HttpServer.response.empty({ status: StatusCodes.BAD_REQUEST })
}),
RedisError: error =>
Effect.gen(function* (_) {
Expand All @@ -112,9 +113,9 @@ export const Router = HttpServer.router.empty.pipe(
),
)

return HttpServer.response.empty({ status: 503 })
return HttpServer.response.empty({ status: StatusCodes.SERVICE_UNAVAILABLE })
}),
RequestError: () => HttpServer.response.empty({ status: 400 }),
RequestError: () => HttpServer.response.empty({ status: StatusCodes.BAD_REQUEST }),
SlackErrorResponse: error =>
Effect.gen(function* (_) {
yield* _(
Expand All @@ -123,11 +124,11 @@ export const Router = HttpServer.router.empty.pipe(
),
)

return HttpServer.response.empty({ status: 503 })
return HttpServer.response.empty({ status: StatusCodes.SERVICE_UNAVAILABLE })
}),
}),
),
),
Effect.catchTag('RouteNotFound', () => HttpServer.response.empty({ status: 404 })),
Effect.catchTag('RouteNotFound', () => HttpServer.response.empty({ status: StatusCodes.NOT_FOUND })),
HttpServer.server.serve(HttpServer.middleware.logger),
)

0 comments on commit 444014b

Please sign in to comment.