payrex-node 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.1.2] - 2024-05-23
4
+
5
+ - Update error handling.
6
+
3
7
  ## [0.1.1] - 2024-05-22
4
8
 
5
9
  - Update README.md
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "payrex-node",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "PayRex Node JS Library",
5
5
  "keywords": [
6
6
  "payrex",
package/src/HttpClient.js CHANGED
@@ -40,11 +40,15 @@ HttpClient.prototype.request = async function ({ path, method, payload }) {
40
40
 
41
41
  return new ApiResource(response.data);
42
42
  } catch (error) {
43
+ if (error.response === undefined) {
44
+ throw new Error(error);
45
+ }
46
+
43
47
  if (error.response.status === 400) {
44
48
  throw new RequestInvalidError(error.response.data);
45
- } else if (error.status === 401) {
49
+ } else if (error.response.status === 401) {
46
50
  throw new AuthenticationInvalidError(error.response.data);
47
- } else if (error.status === 404) {
51
+ } else if (error.response.status === 404) {
48
52
  throw new ResourceNotFoundError(error.response.data);
49
53
  } else {
50
54
  throw new BaseError(error.response.data);
package/test2.js ADDED
@@ -0,0 +1,46 @@
1
+ const payrex = require('payrex-node')(
2
+ 'sk_live_znE26cM4ZbZP3mfTLeZsnw7EceWHKiPN'
3
+ );
4
+
5
+ const RequestInvalidError = require('./src/errors/RequestInvalidError');
6
+ const AuthenticationInvalidError = require('./src/errors/AuthenticationInvalidError');
7
+ const ResourceNotFoundError = require('./src/errors/ResourceNotFoundError');
8
+
9
+ // try {
10
+ // const paymentIntent = await payrex.paymentIntents.create({ amount: 10000 });
11
+ // } catch (error) {
12
+ // console.log('caught error!')
13
+ // if (error instanceof ResourceNotFoundError) {
14
+ // // handle error if a resource does not exist.
15
+ // } else if (error instanceof AuthenticationInvalidError) {
16
+ // // handle authentication error
17
+ // } else if (error instanceof RequestInvalidError) {
18
+ // console.log('handle error if there\'s validation error');
19
+
20
+ // error.errors.forEach((e) => {
21
+ // console.log(e.code);
22
+ // console.log(e.detail);
23
+ // });
24
+ // }
25
+ // }
26
+
27
+ (async () => {
28
+ try {
29
+ const paymentIntent = await payrex.paymentIntents.create({ amount: 10000 });
30
+ } catch (error) {
31
+ console.log('caught error!');
32
+ console.log(error.name);
33
+ if (error.name === 'ResourceNotFoundError') {
34
+ // handle error if a resource does not exist.
35
+ } else if (error.name === 'AuthenticationInvalidError') {
36
+ // handle authentication error
37
+ } else if (error.name === 'RequestInvalidError') {
38
+ console.log("handle error if there's validation error");
39
+
40
+ error.errors.forEach((e) => {
41
+ console.log(e.code);
42
+ console.log(e.detail);
43
+ });
44
+ }
45
+ }
46
+ })();