Skip to content

Commit d353d7c

Browse files
committedDec 27, 2021
Throw a special error subclass when the public IP address could not be found
1 parent e53d7fe commit d353d7c

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed
 

‎browser.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ export class CancelError extends Error {
1111
}
1212
}
1313

14+
export class IpNotFoundError extends Error {
15+
constructor(options) {
16+
super('Could not get the public IP address', options);
17+
this.name = 'IpNotFoundError';
18+
}
19+
}
20+
1421
const defaults = {
1522
timeout: 5000,
1623
};
@@ -83,7 +90,7 @@ const queryHttps = (version, options) => {
8390
}
8491
}
8592

86-
throw new Error('Could not find your IP address', {cause: lastError});
93+
throw new IpNotFoundError({cause: lastError});
8794
})();
8895

8996
promise.cancel = () => {

‎index.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
Thrown when the public IP address could not be found.
3+
*/
4+
export class IpNotFoundError extends Error {}
5+
16
export interface Options {
27
/**
38
Use a HTTPS check using the [icanhazip.com](https://github.com/major/icanhaz) service instead of the DNS query. [ipify.org](https://www.ipify.org) is used as a fallback if `icanhazip.com` fails. This check is much more secure and tamper-proof, but also a lot slower.

‎index.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ import dns from 'dns-socket';
44
import got, {CancelError} from 'got';
55
import isIp from 'is-ip';
66

7+
export class IpNotFoundError extends Error {
8+
constructor(options) {
9+
super('Could not get the public IP address', options);
10+
this.name = 'IpNotFoundError';
11+
}
12+
}
13+
714
const defaults = {
815
timeout: 5000,
916
onlyHttps: false,
@@ -129,7 +136,7 @@ const queryDns = (version, options) => {
129136

130137
socket.destroy();
131138

132-
throw new Error('Could not find your IP address', {cause: lastError});
139+
throw new IpNotFoundError({cause: lastError});
133140
})();
134141

135142
promise.cancel = () => {
@@ -183,8 +190,7 @@ const queryHttps = (version, options) => {
183190
}
184191
}
185192

186-
const errorDescription = lastError ? `: ${lastError.message}` : '';
187-
throw new Error(`Could not find your IP address${errorDescription}`, {cause: lastError});
193+
throw new IpNotFoundError({cause: lastError});
188194
} catch (error) {
189195
// Don't throw a cancellation error for consistency with DNS
190196
if (!(error instanceof CancelError)) {

‎readme.md

+8
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ Default: `5000`
6464

6565
The time in milliseconds until a request is considered timed out.
6666

67+
### IpNotFoundError
68+
69+
Error thrown when the public IP address could not be found.
70+
71+
### CancelError
72+
73+
Error thrown when the operation was canceled.
74+
6775
## Maintainers
6876

6977
- [Sindre Sorhus](https://github.com/sindresorhus)

0 commit comments

Comments
 (0)
Please sign in to comment.