@@ -4,6 +4,9 @@ import { SentryError } from '@sentry/utils/error';
4
4
/** Regular expression used to parse a Dsn. */
5
5
const DSN_REGEX = / ^ (?: ( \w + ) : ) \/ \/ (?: ( \w + ) (?: : ( \w + ) ) ? @ ) ( [ \w \. - ] + ) (?: : ( \d + ) ) ? \/ ( .+ ) / ;
6
6
7
+ /** Error message */
8
+ const ERROR_MESSAGE = 'Invalid Dsn' ;
9
+
7
10
/** The Sentry Dsn, identifying a Sentry instance and project. */
8
11
export class Dsn implements DsnComponents {
9
12
/** Protocol used to connect to Sentry. */
@@ -54,7 +57,7 @@ export class Dsn implements DsnComponents {
54
57
private fromString ( str : string ) : void {
55
58
const match = DSN_REGEX . exec ( str ) ;
56
59
if ( ! match ) {
57
- throw new SentryError ( 'Invalid Dsn' ) ;
60
+ throw new SentryError ( ERROR_MESSAGE ) ;
58
61
}
59
62
60
63
const [ protocol , user , pass = '' , host , port = '' , lastPath ] = match . slice ( 1 ) ;
@@ -81,18 +84,18 @@ export class Dsn implements DsnComponents {
81
84
82
85
/** Validates this Dsn and throws on error. */
83
86
private validate ( ) : void {
84
- for ( const component of [ 'protocol' , 'user' , 'host' , 'projectId' ] ) {
87
+ [ 'protocol' , 'user' , 'host' , 'projectId' ] . forEach ( component => {
85
88
if ( ! this [ component as keyof DsnComponents ] ) {
86
- throw new SentryError ( `Invalid Dsn: Missing ${ component } ` ) ;
89
+ throw new SentryError ( ERROR_MESSAGE ) ;
87
90
}
88
- }
91
+ } ) ;
89
92
90
93
if ( this . protocol !== 'http' && this . protocol !== 'https' ) {
91
- throw new SentryError ( `Invalid Dsn: Unsupported protocol " ${ this . protocol } "` ) ;
94
+ throw new SentryError ( ERROR_MESSAGE ) ;
92
95
}
93
96
94
97
if ( this . port && Number . isNaN ( parseInt ( this . port , 10 ) ) ) {
95
- throw new SentryError ( `Invalid Dsn: Invalid port number " ${ this . port } "` ) ;
98
+ throw new SentryError ( ERROR_MESSAGE ) ;
96
99
}
97
100
}
98
101
}
0 commit comments