Skip to content

question: how to serialize instances of native error classes #1285

Open
@lsg551

Description

@lsg551

Just started using class-transformer and tried to serialize error instances but only got empty objects.

import "reflect-metadata"
import { instanceToPlain } from "class-transformer"

const err = new RangeError("test")
const plain = instanceToPlain(err)

I expected plain to be something like {name: "RangeError", message: "test"} (or with a stack trace message). Unfortunately, it returned an empty object {}.

Inheriting from the Error class like

class TestError extends Error {
    constructor(message: string) {
        super(message)
        this.name = TestError
    }
}

const err = new TestError("test")
const plain = instanceToPlain(err)

includes at least the name prop (it returns {name: "TestError"}) but has no other props at all.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: questionQuestions about the usage of the library.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions