Skip to content

Commit

Permalink
Make metadata access safer at runtime
Browse files Browse the repository at this point in the history
The `!` operator is a type assertion that the attribute is not null, but
does nothing at runtime to ensure this is the case. I haven't yet found
reproduction steps for this failure, but I encountered a case where
accessing `model!.sharedModel` threw an error. This commit adds optional
chaining operators, which do provide protection at runtime.
  • Loading branch information
shreve committed Aug 12, 2024
1 parent 62b57c3 commit 92c805d
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class JupyterlabNotebookCodeFormatter extends JupyterlabCodeFormatter {
}

const metadata =
this.notebookTracker.currentWidget.content.model!.sharedModel.metadata;
this.notebookTracker.currentWidget.content.model?.sharedModel?.metadata;

if (!metadata) {
return null;
Expand Down

0 comments on commit 92c805d

Please sign in to comment.