From 0b75fe938776c90f77593760d990be17c4415171 Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Mon, 13 Jan 2025 16:06:01 +0000 Subject: [PATCH] Prefer Effect's error types Refs #1834 --- src/GhostPage.ts | 8 ++++++-- src/Program.ts | 24 +++++++++++++++++------- test/AboutUsPage/AboutUsPage.test.ts | 4 ++-- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/GhostPage.ts b/src/GhostPage.ts index 0a57c86a0..367fc6801 100644 --- a/src/GhostPage.ts +++ b/src/GhostPage.ts @@ -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 + (id: string) => Effect.Effect >() {} diff --git a/src/Program.ts b/src/Program.ts index d94acfaa2..709e53288 100644 --- a/src/Program.ts +++ b/src/Program.ts @@ -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' @@ -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, + ), + ), + ) }), ), ), diff --git a/test/AboutUsPage/AboutUsPage.test.ts b/test/AboutUsPage/AboutUsPage.test.ts index 28c314e1d..a2cc1003c 100644 --- a/test/AboutUsPage/AboutUsPage.test.ts +++ b/test/AboutUsPage/AboutUsPage.test.ts @@ -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' @@ -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* () {