question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature] Provide better message when using getProp

See original GitHub issue

I have a checker and using it to check some of it props

const propertyName: string = nameof<MyModel>((o) => o.headlines);
// propertyName = 'headlines'
// value is an array / should be [string, string, string?]
MyModelChecker.getProp(propertyName).strictCheck(value);

I provoked an error and the result was

Error: value[0] is not a string
    at new VError (util.js?5394:20)
    at DetailContext.getError (util.js?5394:91)
    at Checker._doCheck (index.js?7160:179)
    at Checker.strictCheck (index.js?7160:101)
    at validator (MyComponent.ts?b499:24)
    at assertProp (vue.esm.js?a026:1722)
    at validateProp (vue.esm.js?a026:1641)
    at loop (vue.esm.js?a026:4668)
    at initProps (vue.esm.js?a026:4701)
    at initState (vue.esm.js?a026:4642)

Could we provide the parameter value of getProp in the message, so I could see which property is checked?

The message could be headlines[0] is not a string or it could be MyModelChecker's headlines found: value[0] is not a string or similar

I’m open to discussion and I’m willing to implement this feature once we’ve found a good design

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
dsagalcommented, Aug 29, 2019

I added setReportedPath() method. E.g.

MyModelChecker.setReportedPath('MyModel');
MyModelChecker.getProp(propertyName).strictCheck(value);

Should report, on error, MyModel.headlines[0] is not a string. I released a patch version 0.1.9.

1reaction
dsagalcommented, Aug 28, 2019

This is not unreasonable, in fact, including a diff below which might be what you are after. On the other hand, if the caller is using getProp, then the property is name available, so perhaps it’s OK (better?) to leave it to the caller to format errors appropriately.

E.g. in your case, you could add a helper:

function getCheckerFunc(rootChecker: Checker, propertyName: string) {
  const checker = rootChecker.getProp(propertyName);
  return (value: any) => {
    try {
      checker.strictCheck(value);
    } catch (e) {
      e.message = `Property '${propertyName}': ${e.message}`;
      throw e;
    }
  };
}

Diff of possible implementation changing getProp() to throw errors of the form propName is not a ...:

diff --git a/lib/index.ts b/lib/index.ts
index 46490b3..b59d106 100644
--- a/lib/index.ts
+++ b/lib/index.ts
@@ -43,7 +43,7 @@ export class Checker {
   private checkerStrict: CheckerFunc;

   // Create checkers by using `createCheckers()` function.
-  constructor(private suite: ITypeSuite, private ttype: TType) {
+  constructor(private suite: ITypeSuite, private ttype: TType, private _path?: string) {
     if (ttype instanceof TIface) {
       for (const p of ttype.props) {
         this.props.set(p.name, p.ttype);
@@ -104,7 +104,7 @@ export class Checker {
   public getProp(prop: string): Checker {
     const ttype = this.props.get(prop);
     if (!ttype) { throw new Error(`Type has no property ${prop}`); }
-    return new Checker(this.suite, ttype);
+    return new Checker(this.suite, ttype, prop);
   }

   /**
@@ -160,7 +160,7 @@ export class Checker {
     if (!checkerFunc(value, noopCtx)) {
       const detailCtx = new DetailContext();
       checkerFunc(value, detailCtx);
-      throw detailCtx.getError();
+      throw detailCtx.getError(this._path);
     }
   }

@@ -171,7 +171,7 @@ export class Checker {
     }
     const detailCtx = new DetailContext();
     checkerFunc(value, detailCtx);
-    return detailCtx.getErrorDetail();
+    return detailCtx.getErrorDetail(this._path);
   }

   private _getMethod(methodName: string): TFunc {
diff --git a/lib/util.ts b/lib/util.ts
index d909f93..cab6a83 100644
--- a/lib/util.ts
+++ b/lib/util.ts
@@ -83,8 +83,7 @@ export class DetailContext implements IContext {
     }
   }

-  public getError(): VError {
-    let path: string = "value";
+  public getError(path: string = "value"): VError {
     const msgParts: string[] = [];
     for (let i = this._propNames.length - 1; i >= 0; i--) {
       const p = this._propNames[i];
@@ -97,8 +96,7 @@ export class DetailContext implements IContext {
     return new VError(path, msgParts.join("; "));
   }

-  public getErrorDetail(): IErrorDetail|null {
-    let path: string = "value";
+  public getErrorDetail(path: string = "value"): IErrorDetail|null {
     const details: IErrorDetail[] = [];
     for (let i = this._propNames.length - 1; i >= 0; i--) {
       const p = this._propNames[i];
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Window Properties - Win32 apps - Microsoft Learn
A window can create handles to its window property data and use the data for any purpose. The following example uses GetProp to...
Read more >
How to define and use a system property in Android ...
I noticed that I can read system properties with System.getProperty() function. So I use setprop command to set a system property. For example:...
Read more >
Adb useful commands list - gists · GitHub
Adb useful commands list. GitHub Gist: instantly share code, notes, and snippets.
Read more >
OpenLayers v7.2.2 API - Class: Feature
A vector object for geographic features with a geometry and other attribute properties, similar to the features in vector file formats like GeoJSON....
Read more >
dumpsys - Android Developers
This output is typically more verbose than you want, so use the command-line options on this page to get output for only the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found