Effect CommunityEC
Effect Community3y ago
11 replies
Sly

Any best practices on when to choose which way to declare errors?

There seem to be multiple ways of declaring errors, such as:

class RequestError {
  readonly _tag = "RequestError"
  constructor(readonly error: unknown) {}
}

or
const RequestError_ = Schema.struct({ _tag: Schema.literal("RequestError") })
export interface RequestError extends Schema.To<typeof RequestError_> {}
export const RequestError: Schema.Schema<RequestError> = Schema.to(RequestError_)


Are there any relevant differences between the two approaches?

W.r.t. to the latter one as it's quite verbose one could use the snippet:

    "Err": {
        "prefix": "ee",
        "body": [
            "const ${1:errorName}_ = Schema.struct({ _tag: Schema.literal('${1:errorName}') })",
            "export interface ${1:errorName} extends Schema.To<typeof ${1:errorName}_> {}",
            "export const ${1:errorName}: Schema.Schema<${1:errorName}> = Schema.to(${1:errorName}_)$0"
        ],
        "description": "Generic error template"
    }
Was this page helpful?