Effect CommunityEC
Effect Community2y ago
35 replies
sign error

Error Handling in Schema Transform

I am creating a Schema from a transform. The situation is like that:

// INCOMPLETE CODE
// SHORTENED TO ILLUSTRATE QUESTION,
// which is near the bottom

// FROM
type InputNode = {
  name: string // unique
  parentNodes: string[] // refers to other InputNode names
}

type InputNodeList = readonly InputNode []


// TO
type Node = {
  id: NodeID // Branded Type
  data: InputNode
  … // maybe more later on
}

type Link = { source: NodeID, target: NodeID }

const _Graph = S.struct({ nodes: S.array(Node) , links: S.array(Link) })


const Graph = transform(
  InputNodeList,
  _Grapth,
  // QUESTION HERE
  iNodeList => {
    // 1. Have to decode the Node List first in order to,
    // 2. derive the the Link list from it
    // here is my first try using an Either:
    const nodes = S.decodeEither(S.array(Node))(iNodeList);
    nodes.pipe(E.flatMap(ns => /* decode more here */  ))
    // HOW TO HANDLE ERRORS HERE?
    // throw an error??
  },
  …
)


How would I properly implement the »sub-decodings« such that I can handle Errors as well. What's the way to go here??
Was this page helpful?