Repository: pactumjs/pactum (js)

Issue:
Custom expect handler does not print request and response when the assertion fails.
**Describe the bug**
Most expect methods print the request and response when the assertion fails.
However, the custom expect handler does not show these objects.

**To Reproduce**
Steps to reproduce the behavior:
```javascript
const spec = pactum.spec();
spec.get(url);
await spec.toss();
await spec.response().to.have._((ctx) => {
  assert.fail('error');
});
```

**Expected behavior**
It should show the request and response as other methods do.

**Software (please complete the following information):**
 - OS: Linux
 - NodeJS: 16.1
 - Pactum: 3.0.19


Pull request description:
fix: error printing and withJson invalid file
closes #83 

Patch:
diff --git a/src/exports/expect.js b/src/exports/expect.js
index 4c2668e..f6f8e58 100644
--- a/src/exports/expect.js
+++ b/src/exports/expect.js
@@ -108,14 +108,18 @@ class Have {
 
   _validate() {
     try {
-      return this.expect.validate({}, this.response);
+      return this.expect.validate({}, this.response).catch((err) => { this._handle_validate_error(err) });
     } catch (error) {
-      if (this.spec && this.spec.status !== 'FAILED') {
-        this.spec.status = 'FAILED';
-        utils.printReqAndRes(this.spec._request, this.response);
-      }
-      throw error;
+      return this._handle_validate_error(error);
+    }
+  }
+
+  _handle_validate_error(error) {
+    if (this.spec && this.spec.status !== 'FAILED') {
+      this.spec.status = 'FAILED';
+      utils.printReqAndRes(this.spec._request, this.response);
     }
+    throw error;
   }
 
 }
diff --git a/src/helpers/file.utils.js b/src/helpers/file.utils.js
index 588db26..2049198 100644
--- a/src/helpers/file.utils.js
+++ b/src/helpers/file.utils.js
@@ -33,6 +33,14 @@ function saveSnapshot(name, data) {
  * @param {string} name 
  */
 function findFile(name, dir = config.data.dir) {
+  const result = _findFile(name, dir);
+  if (result) {
+    return result;
+  }
+  throw new Error(`File Not Found - '${name}'`);
+}
+
+function _findFile(name, dir = config.data.dir) {
   if (fs.existsSync(name)) {
     return fs.readFileSync(name);
   }
@@ -47,7 +55,7 @@ function findFile(name, dir = config.data.dir) {
       const dirPath = path.resolve(dir, file);
       const stats = fs.statSync(dirPath);
       if (stats.isDirectory()) {
-        const result = findFile(name, dirPath);
+        const result = _findFile(name, dirPath);
         if (result) {
           return result;
         }


Test patch:
diff --git a/test/component/expects.custom.spec.js b/test/component/expects.custom.spec.js
new file mode 100644
index 0000000..7c1c9b4
--- /dev/null
+++ b/test/component/expects.custom.spec.js
@@ -0,0 +1,37 @@
+const { spec } = require('../../src');
+const assert = require('assert');
+const expect = require('chai').expect;
+
+describe('Expects - Custom', () => {
+
+  it('custom expect handler - fails', async () => {
+    let err;
+    try {
+      await spec()
+        .useInteraction('default get')
+        .get('http://localhost:9393/default/get')
+        .expect((_) => { assert.fail('error'); })
+        .expectStatus(200);
+    } catch (error) {
+      err = error;
+    }
+    expect(err.message).equals(`error`);
+  });
+
+  it('custom expect handler - bdd - fails', async () => {
+    let err;
+    try {
+      const _spec = spec();
+      await _spec
+        .useInteraction('default get')
+        .get('http://localhost:9393/default/get')
+        .expectStatus(200);
+      await _spec.response().to.have._((_) => {
+        assert.fail('error');
+      });
+    } catch (error) {
+      err = error;
+    }
+    expect(err.message).equals(`error`);
+  });
+})
\ No newline at end of file
diff --git a/test/component/withJson.spec.js b/test/component/withJson.spec.js
index b3a7577..ad15793 100644
--- a/test/component/withJson.spec.js
+++ b/test/component/withJson.spec.js
@@ -1,4 +1,5 @@
 const { spec, settings } = require('../../src/index');
+const expect = require('chai').expect;
 
 describe('withJson', () => {
 
@@ -47,5 +48,17 @@ describe('withJson', () => {
       .withJson('sample-2.json')
       .expectStatus(200);
   });
+
+  it('with invalid file', async () => {
+    let err;
+    try {
+      await spec()
+        .post('http://localhost:9393/file')
+        .withJson('invalid-file.json')
+    } catch(error) {
+      err = error;
+    }
+    expect(err.message).equals(`File Not Found - 'invalid-file.json'`);
+  })
   
 });
\ No newline at end of file