|
5 | 5 | const utils = require('./utils.js'); |
6 | 6 | const jsonParser = require('./lib/json-parser'); |
7 | 7 | const libGetEventStreamed = require('./lib/getEventStreamed'); |
| 8 | +const PryvError = require('./lib/PryvError'); |
8 | 9 |
|
9 | 10 | /** |
10 | 11 | * @class Connection |
@@ -58,10 +59,10 @@ class Connection { |
58 | 59 | async username () { |
59 | 60 | const accessInfo = await this.accessInfo(); |
60 | 61 | if (accessInfo.error) { |
61 | | - const err = new Error('Failed fetching accessinfo: ' + accessInfo.error.message); |
62 | | - // @ts-ignore - custom error property |
63 | | - err.innerObject = accessInfo.error; |
64 | | - throw err; |
| 62 | + throw new PryvError( |
| 63 | + 'Failed fetching accessinfo: ' + accessInfo.error.message, |
| 64 | + accessInfo.error |
| 65 | + ); |
65 | 66 | } |
66 | 67 | // @ts-ignore - username is always a string |
67 | 68 | return accessInfo.user.username; |
@@ -110,15 +111,13 @@ class Connection { |
110 | 111 | result[0].error || |
111 | 112 | (expectedKey != null && result[0][expectedKey] == null) |
112 | 113 | ) { |
113 | | - const innerObject = result[0]?.error || result; |
114 | | - const error = new Error( |
| 114 | + const innerError = result[0]?.error || result; |
| 115 | + throw new PryvError( |
115 | 116 | `Error for api method: "${method}" with params: ${JSON.stringify( |
116 | 117 | params |
117 | | - )} >> Result: ${JSON.stringify(innerObject)}"` |
| 118 | + )} >> Result: ${JSON.stringify(innerError)}"`, |
| 119 | + innerError |
118 | 120 | ); |
119 | | - // @ts-ignore - custom error property |
120 | | - error.innerObject = innerObject; |
121 | | - throw error; |
122 | 121 | } |
123 | 122 | if (expectedKey != null) return result[0][expectedKey]; |
124 | 123 | return result[0]; |
|
0 commit comments