Skip to content

Commit dec388d

Browse files
committed
feat(notion): improve errors handling
1 parent 7663f0a commit dec388d

File tree

6 files changed

+21
-39
lines changed

6 files changed

+21
-39
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type * as core from '@contentlayer/core'
2+
import { Tagged } from '@contentlayer/utils/effect'
3+
4+
export class ComputedValueError extends Tagged('ComputedValueError')<{
5+
readonly error: unknown
6+
readonly documentTypeDef: core.DocumentTypeDef
7+
readonly document: core.Document
8+
}> {}

packages/@contentlayer/source-notion/src/fetchData/fetchAllDocuments.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ export const fetchAllDocuments = ({ databaseTypeDefs, schemaDef, options }: Fetc
4040
T.chain((chunks) => T.reduce_(chunks, [] as DataCache.CacheItem[], (z, a) => T.succeed([...z, ...a]))),
4141
),
4242
),
43-
4443
T.map((chunks) => Chunk.reduce_(chunks, [] as DataCache.CacheItem[], (z, a) => [...z, ...a])),
4544
T.map((documents) => ({ cacheItemsMap: Object.fromEntries(documents.map((_) => [_.document._id, _])) })),
4645
OT.withSpan('@contentlayer/source-notion/fetchData:fetchAllDocuments'),

packages/@contentlayer/source-notion/src/fetchData/getComputedValues.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type * as core from '@contentlayer/core'
22
import { OT, pipe, T } from '@contentlayer/utils/effect'
33

4+
import { ComputedValueError } from './errors.js'
5+
46
export type GetComputedValuesArgs = {
57
document: core.Document
68
documentTypeDef: core.DocumentTypeDef
@@ -13,7 +15,7 @@ export const getComputedValues = ({ document, documentTypeDef }: GetComputedValu
1315
mapValue: (field) =>
1416
T.tryCatchPromise(
1517
async () => field.resolve(document),
16-
() => new Error('TODO: Make error'),
18+
(error) => new ComputedValueError({ error, documentTypeDef, document }),
1719
),
1820
}),
1921
OT.withSpan('@contentlayer/source-notion/fetchData:getComputedValues'),

packages/@contentlayer/source-notion/src/fetchData/makeCacheItem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const makeCacheItem = ({ databaseTypeDef, documentTypeDef, page, options
1919
T.gen(function* ($) {
2020
const document = yield* $(makeDocument({ documentTypeDef, databaseTypeDef, page, options }))
2121

22-
const computedValues = yield $(getComputedValues({ document, documentTypeDef }))
22+
const computedValues = yield* $(getComputedValues({ document, documentTypeDef }))
2323

2424
Object.entries(computedValues).forEach(([fieldName, value]) => {
2525
document[fieldName] = value

packages/@contentlayer/source-notion/src/mapping/index.ts

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type * as core from '@contentlayer/core'
22
import type { Has } from '@contentlayer/utils/effect'
3-
import { pipe, T } from '@contentlayer/utils/effect'
3+
import { T } from '@contentlayer/utils/effect'
44
import type { NotionRenderer } from '@notion-render/client'
55
import type * as notion from '@notionhq/client'
66

@@ -66,7 +66,6 @@ export type FieldFunctions<T extends DatabasePropertyTypes = DatabasePropertyTyp
6666
}
6767

6868
type FieldMappingType = {
69-
// TODO : Remove optional
7069
[key in DatabasePropertyTypes]: FieldFunctions
7170
}
7271

@@ -93,43 +92,18 @@ const FieldMapping: FieldMappingType = {
9392
rollup: fieldRollup,
9493
}
9594

96-
export const getFieldFunctions = <T extends DatabasePropertyTypes = DatabasePropertyTypes>(
97-
type: DatabasePropertyTypes,
98-
) =>
99-
pipe(
100-
T.sync(() => FieldMapping[type] as FieldFunctions<T> | undefined),
101-
102-
T.chain((func) =>
103-
T.cond_(
104-
!!func,
105-
() => func!,
106-
() => 'fail' as const, // TODO : Error
107-
),
108-
),
109-
)
110-
11195
export const getFieldDef = <T extends DatabasePropertyTypes>(
11296
args: { property: DatabaseProperties } & Omit<GetFieldDefArgs<T>, 'propertyData'>,
11397
) =>
114-
pipe(
115-
getFieldFunctions(args.property.type),
116-
T.chain((functions) =>
117-
functions.getFieldDef({
118-
propertyData: getDatabasePropertyData(args.property),
119-
...args,
120-
}),
121-
),
122-
)
98+
FieldMapping[args.property.type].getFieldDef({
99+
propertyData: getDatabasePropertyData(args.property),
100+
...args,
101+
})
123102

124103
export const getFieldData = <T extends PagePropertyTypes>(
125104
args: { property: PageProperties } & Omit<GetFieldDataArgs<T>, 'propertyData'>,
126105
) =>
127-
pipe(
128-
getFieldFunctions(args.property.type),
129-
T.chain((functions) =>
130-
functions.getFieldData({
131-
propertyData: getPagePropertyData(args.property),
132-
...args,
133-
}),
134-
),
135-
)
106+
FieldMapping[args.property.type].getFieldData({
107+
propertyData: getPagePropertyData(args.property),
108+
...args,
109+
})

packages/@contentlayer/source-notion/src/schema/provideFieldDef.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,5 @@ export const provideFieldDef = ({ property, databaseTypeDef, getDocumentTypeDef
3131
} as FieldDef
3232
}),
3333
),
34-
T.catchAll(() => T.succeed(undefined)), // TODO : Better error handling
3534
OT.withSpan('@contentlayer/source-notion/schema:provideFieldDef'),
3635
)

0 commit comments

Comments
 (0)