You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The returned `identity` and `psk`**have to be stored** for future connections to the gateway. To comply with IKEA's requests, the security code **must not be stored** permanently in your application.
196
200
197
-
The call throws an error if it wasn't successful which you should handle. The error `e` should be of type `TradfriError` and gives further information why the authentication failed. To check that, add `TradfriError` and `TradfriErrorCodes` to the list of imports and check as follows:
201
+
If the authentication was not successful, this method throws (or rather rejects with) an error which you should handle. The error `e` should be of type `TradfriError` and gives further information why the authentication failed. To check that, add `TradfriError` and `TradfriErrorCodes` to the list of imports and check as follows:
198
202
```TS
199
203
if (einstanceofTradfriError) {
200
204
switch (e.code) {
201
-
caseTradfriErrorCodes.ConnectionFailed: {
202
-
//Gateway unreachable or security code wrong
205
+
caseTradfriErrorCodes.ConnectionTimedOut: {
206
+
//The gateway is unreachable or did not respond in time
203
207
}
204
208
caseTradfriErrorCodes.AuthenticationFailed: {
205
-
// Something went wrong with the authentication.
206
-
// It might be that this library has to be updated to be compatible with a new firmware.
209
+
// The security code is wrong or something else went wrong with the authentication.
210
+
// Check the error message for details. It might be that this library has to be updated
211
+
// to be compatible with a new firmware.
212
+
}
213
+
caseTradfriErrorCodes.ConnectionFailed: {
214
+
// An unknown error happened while trying to connect
207
215
}
208
216
}
209
217
}
@@ -212,9 +220,24 @@ if (e instanceof TradfriError) {
212
220
### Connecting to the gateway
213
221
When you have a valid identity and psk, you can connect to the gateway using the `connect` method:
// The gateway is unreachable or did not respond in time
230
+
}
231
+
caseTradfriErrorCodes.AuthenticationFailed: {
232
+
// The provided credentials are not valid. You need to re-authenticate using `authenticate()`.
233
+
}
234
+
caseTradfriErrorCodes.ConnectionFailed: {
235
+
// An unknown error happened while trying to connect
236
+
}
237
+
}
238
+
}
216
239
```
217
-
If the connection was unsuccessful, either the gateway was unreachable or the identity/psk pair isn't valid.
240
+
**NOTE:** As of v0.6.0, this no longer resolves with `false` if the connection was unsuccessful. Instead, it throws (or rejects with) a `TradfriError` which contains details about why the connection failed.
218
241
219
242
### Pinging the gateway
220
243
```TS
@@ -507,7 +530,9 @@ A DeviceInfo object contains general information about a device. It has the foll
507
530
508
531
## Changelog
509
532
510
-
#### __WORK IN PROGRESS__
533
+
#### __WORK IN PROGRESS__ - WARNING: BREAKING CHANGES!
534
+
* (AlCalzone) **BREAKING**: The `connect()` method now either resolves with `true` or rejects with an error detailing why the connection failed.
535
+
* (AlCalzone) The error thrown by `authentication()` now correctly reflects why the authentication failed.
511
536
* (AlCalzone) Swallow `"DTLS handshake timed out"` promise rejections and emit an `"error"` instead
Copy file name to clipboardExpand all lines: build/tradfri-client.js
+19-5Lines changed: 19 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -54,7 +54,14 @@ class TradfriClient extends events_1.EventEmitter {
54
54
* @param psk The pre-shared key belonging to the identity.
55
55
*/
56
56
connect(identity,psk){
57
-
returnthis.tryToConnect(identity,psk);
57
+
return__awaiter(this,void0,void0,function*(){
58
+
switch(yieldthis.tryToConnect(identity,psk)){
59
+
casetrue: returntrue;
60
+
case"auth failed": thrownewtradfri_error_1.TradfriError("The provided credentials are not valid. Please re-authenticate!",tradfri_error_1.TradfriErrorCodes.AuthenticationFailed);
61
+
case"timeout": thrownewtradfri_error_1.TradfriError("The gateway did not respond in time.",tradfri_error_1.TradfriErrorCodes.ConnectionTimedOut);
62
+
case"error": thrownewtradfri_error_1.TradfriError("An unknown error occured while connecting to the gateway",tradfri_error_1.TradfriErrorCodes.ConnectionFailed);
63
+
}
64
+
});
58
65
}
59
66
/**
60
67
* Try to establish a connection to the configured gateway.
@@ -71,7 +78,12 @@ class TradfriClient extends events_1.EventEmitter {
case"auth failed": thrownewtradfri_error_1.TradfriError("The security code is wrong",tradfri_error_1.TradfriErrorCodes.AuthenticationFailed);
103
+
case"timeout": thrownewtradfri_error_1.TradfriError("The gateway did not respond in time.",tradfri_error_1.TradfriErrorCodes.ConnectionTimedOut);
104
+
case"error": thrownewtradfri_error_1.TradfriError("An unknown error occured while connecting to the gateway",tradfri_error_1.TradfriErrorCodes.ConnectionFailed);
0 commit comments