Simplifying Error Handling with Effect.tryPromise
hi, is there any better way to do the same? (not the logic in
trytry)Effect.tryPromise({
try: async () => {
if (this.user.shortLinks) {
return this.user.shortLinks
}
await this.user.loadOnce('shortLinks')
if (this.user.shortLinks) {
return this.user.shortLinks
}
},
catch: LucidModelRelationshipError.fromUnknownError(User.name, ShortLink.name, 'shortLinks'),
}).pipe(
Effect.flatMap(_ => Effect.gen(function* () {
if (is.nullOrUndefined(_)) {
return yield* new LucidModelRelationshipError({ model: User.name, relationship: 'shortLinks', relatedModel: ShortLink.name })
}
return _
})),
)Effect.tryPromise({
try: async () => {
if (this.user.shortLinks) {
return this.user.shortLinks
}
await this.user.loadOnce('shortLinks')
if (this.user.shortLinks) {
return this.user.shortLinks
}
},
catch: LucidModelRelationshipError.fromUnknownError(User.name, ShortLink.name, 'shortLinks'),
}).pipe(
Effect.flatMap(_ => Effect.gen(function* () {
if (is.nullOrUndefined(_)) {
return yield* new LucidModelRelationshipError({ model: User.name, relationship: 'shortLinks', relatedModel: ShortLink.name })
}
return _
})),
)