Skip to content

Commit

Permalink
Prefer Effect's error types
Browse files Browse the repository at this point in the history
Refs #1834
  • Loading branch information
thewilkybarkid committed Jan 13, 2025
1 parent 2a2b259 commit 0b75fe9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/GhostPage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Context, type Effect } from 'effect'
import { Context, Data, type Effect } from 'effect'
import type { Html } from './html.js'

export class PageIsNotFound extends Data.TaggedError('PageIsNotFound') {}

export class PageIsUnavailable extends Data.TaggedError('PageIsUnavailable') {}

export class GetPageFromGhost extends Context.Tag('GetPageFromGhost')<
GetPageFromGhost,
(id: string) => Effect.Effect<Html, 'unavailable' | 'not-found'>
(id: string) => Effect.Effect<Html, PageIsUnavailable | PageIsNotFound>
>() {}
24 changes: 17 additions & 7 deletions src/Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { collapseRequests, logFetch } from './fetch.js'
import * as FptsToEffect from './FptsToEffect.js'
import { getPreprint as getPreprintUtil } from './get-preprint.js'
import { getPage, GhostApi } from './ghost.js'
import { GetPageFromGhost } from './GhostPage.js'
import * as GhostPage from './GhostPage.js'
import { html } from './html.js'
import * as Keyv from './keyv.js'
import { getPseudonymFromLegacyPrereview } from './legacy-prereview.js'
Expand Down Expand Up @@ -360,15 +360,25 @@ export const Program = pipe(
),
Layer.effect(Comments.GetComment, Comments.makeGetComment),
Layer.effect(
GetPageFromGhost,
GhostPage.GetPageFromGhost,
Effect.gen(function* () {
const fetch = yield* FetchHttpClient.Fetch
const ghostApi = yield* GhostApi
return id =>
FptsToEffect.readerTaskEither(getPage(id), {
fetch,
ghostApi,
})
return flow(
id =>
FptsToEffect.readerTaskEither(getPage(id), {
fetch,
ghostApi,
}),
Effect.mapError(
flow(
Match.value,
Match.when('not-found', () => new GhostPage.PageIsNotFound()),
Match.when('unavailable', () => new GhostPage.PageIsUnavailable()),
Match.exhaustive,
),
),
)
}),
),
),
Expand Down
4 changes: 2 additions & 2 deletions test/AboutUsPage/AboutUsPage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Effect, TestContext } from 'effect'
import { Status } from 'hyper-ts'
import * as _ from '../../src/AboutUsPage/index.js'
import { Locale } from '../../src/Context.js'
import { GetPageFromGhost } from '../../src/GhostPage.js'
import { GetPageFromGhost, PageIsNotFound, PageIsUnavailable } from '../../src/GhostPage.js'
import * as Routes from '../../src/routes.js'
import * as fc from '../fc.js'

Expand All @@ -31,7 +31,7 @@ describe('AboutUsPage', () => {
),
)

test.prop([fc.supportedLocale(), fc.constantFrom('unavailable', 'not-found')])(
test.prop([fc.supportedLocale(), fc.oneof(fc.constant(new PageIsUnavailable()), fc.constant(new PageIsNotFound()))])(
'when the page cannot be loaded',
async (locale, error) =>
Effect.gen(function* () {
Expand Down

0 comments on commit 0b75fe9

Please sign in to comment.