Skip to content

Commit d2a4a7f

Browse files
author
Perki
committed
added PryvError
1 parent d75b4fc commit d2a4a7f

File tree

5 files changed

+59
-13
lines changed

5 files changed

+59
-13
lines changed

components/pryv/src/Connection.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
const utils = require('./utils.js');
66
const jsonParser = require('./lib/json-parser');
77
const libGetEventStreamed = require('./lib/getEventStreamed');
8+
const PryvError = require('./lib/PryvError');
89

910
/**
1011
* @class Connection
@@ -58,10 +59,10 @@ class Connection {
5859
async username () {
5960
const accessInfo = await this.accessInfo();
6061
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+
);
6566
}
6667
// @ts-ignore - username is always a string
6768
return accessInfo.user.username;
@@ -110,15 +111,13 @@ class Connection {
110111
result[0].error ||
111112
(expectedKey != null && result[0][expectedKey] == null)
112113
) {
113-
const innerObject = result[0]?.error || result;
114-
const error = new Error(
114+
const innerError = result[0]?.error || result;
115+
throw new PryvError(
115116
`Error for api method: "${method}" with params: ${JSON.stringify(
116117
params
117-
)} >> Result: ${JSON.stringify(innerObject)}"`
118+
)} >> Result: ${JSON.stringify(innerError)}"`,
119+
innerError
118120
);
119-
// @ts-ignore - custom error property
120-
error.innerObject = innerObject;
121-
throw error;
122121
}
123122
if (expectedKey != null) return result[0][expectedKey];
124123
return result[0];

components/pryv/src/globals.d.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,13 @@ declare global {
6363
export type LoginButton = PryvModule.LoginButton;
6464
}
6565

66-
// Extended Error type with innerObject property
67-
interface Error {
68-
innerObject?: any;
66+
/**
67+
* Custom error class for Pryv library errors
68+
*/
69+
class PryvError extends Error {
70+
constructor(message: string, innerError?: Error | object);
71+
name: 'PryvError';
72+
innerError?: Error | object;
6973
}
7074
}
7175

components/pryv/src/index.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,16 @@ declare module 'pryv' {
164164
subErrors?: Error[];
165165
};
166166

167+
/**
168+
* Custom error class for Pryv library errors.
169+
* Includes an innerError property for wrapping underlying errors.
170+
*/
171+
export class PryvError extends globalThis.Error {
172+
constructor(message: string, innerError?: globalThis.Error | object);
173+
name: 'PryvError';
174+
innerError?: globalThis.Error | object;
175+
}
176+
167177
export type StreamsQuery = {
168178
any?: Identifier[];
169179
all?: Identifier[];
@@ -991,6 +1001,7 @@ declare module 'pryv' {
9911001
cleanURLFromPrYvParams(url: string): string;
9921002
getQueryParamsFromURL(url: string): KeyValue;
9931003
};
1004+
PryvError: typeof PryvError;
9941005
version: version;
9951006
};
9961007

components/pryv/src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
* @property {pryv.Connection} Connection - To interact with an individual's (user) data set
1010
* @property {pryv.Browser} Browser - Browser Tools - Access request helpers and visuals (button)
1111
* @property {pryv.utils} utils - Exposes some utils for HTTP calls and tools to manipulate Pryv's API endpoints
12+
* @property {pryv.PryvError} PryvError - Custom error class with innerError support
1213
*/
1314
module.exports = {
1415
Service: require('./Service'),
1516
Connection: require('./Connection'),
1617
Auth: require('./Auth'),
1718
Browser: require('./Browser'),
1819
utils: require('./utils'),
20+
PryvError: require('./lib/PryvError'),
1921
version: require('../package.json').version
2022
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @license
3+
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4+
*/
5+
6+
/**
7+
* Custom error class for Pryv library errors.
8+
* Includes an innerError property for wrapping underlying errors.
9+
* @extends Error
10+
*/
11+
class PryvError extends Error {
12+
/**
13+
* Create a PryvError
14+
* @param {string} message - Error message
15+
* @param {Error|Object} [innerError] - The underlying error or object that caused this error
16+
*/
17+
constructor (message, innerError) {
18+
super(message);
19+
this.name = 'PryvError';
20+
/** @type {Error|Object|undefined} */
21+
this.innerError = innerError;
22+
23+
// Maintains proper stack trace for where error was thrown (only in V8)
24+
if (Error.captureStackTrace) {
25+
Error.captureStackTrace(this, PryvError);
26+
}
27+
}
28+
}
29+
30+
module.exports = PryvError;

0 commit comments

Comments
 (0)