Skip to content

Commit

Permalink
Merge pull request #139 from gbmoretti/deserialize-error-dx
Browse files Browse the repository at this point in the history
Throws a descriptive error when Arbor fails to find the type
  • Loading branch information
drborges authored Nov 1, 2024
2 parents b462e44 + 754a99f commit 42b9f3b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
Binary file modified .yarn/install-state.gz
Binary file not shown.
2 changes: 2 additions & 0 deletions packages/arbor-json/src/Json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ export class Json {
} else if (Type) {
const instance = new Type()
return Object.assign(instance, value.$value)
} else {
throw new Error(`Unknown type: ${value.$type}. Can't deserialize.`)
}
}

Expand Down
9 changes: 9 additions & 0 deletions packages/arbor-json/tests/Json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ describe("Json", () => {
expect(deserialized).toBeInstanceOf(TodoList)
expect(deserialized).toEqual(todoList)
})

it("throws exception when custom type is not found", () => {
const serialized =
'{"$value":{"uuid":"a","text":"Clean the house"},"$type":"Task"}'

expect(() => parse(serialized)).toThrow(
"Unknown type: Task. Can't deserialize."
)
})
})

describe("custom type with custom $type key", () => {
Expand Down

0 comments on commit 42b9f3b

Please sign in to comment.