@@ -31,13 +31,14 @@ export function dsnToString(dsn: DsnComponents, withPassword: boolean = false):
3131 * Parses a Dsn from a given string.
3232 *
3333 * @param str A Dsn as string
34- * @returns Dsn as DsnComponents
34+ * @returns Dsn as DsnComponents or undefined if @param str is not a valid DSN string
3535 */
36- export function dsnFromString ( str : string ) : DsnComponents {
36+ export function dsnFromString ( str : string ) : DsnComponents | undefined {
3737 const match = DSN_REGEX . exec ( str ) ;
3838
3939 if ( ! match ) {
40- throw new SentryError ( `Invalid Sentry Dsn: ${ str } ` ) ;
40+ logger . error ( `Invalid Sentry Dsn: ${ str } ` ) ;
41+ return undefined ;
4142 }
4243
4344 const [ protocol , publicKey , pass = '' , host , port = '' , lastPath ] = match . slice ( 1 ) ;
@@ -72,30 +73,39 @@ function dsnFromComponents(components: DsnComponents): DsnComponents {
7273 } ;
7374}
7475
75- function validateDsn ( dsn : DsnComponents ) : boolean | void {
76+ function validateDsn ( dsn : DsnComponents ) : boolean {
7677 if ( ! __DEBUG_BUILD__ ) {
77- return ;
78+ return true ;
7879 }
7980
8081 const { port, projectId, protocol } = dsn ;
8182
8283 const requiredComponents : ReadonlyArray < keyof DsnComponents > = [ 'protocol' , 'publicKey' , 'host' , 'projectId' ] ;
83- requiredComponents . forEach ( component => {
84+ const hasMissingRequiredComponent = requiredComponents . find ( component => {
8485 if ( ! dsn [ component ] ) {
85- throw new SentryError ( `Invalid Sentry Dsn: ${ component } missing` ) ;
86+ logger . error ( `Invalid Sentry Dsn: ${ component } missing` ) ;
87+ return true ;
8688 }
89+ return false ;
8790 } ) ;
8891
92+ if ( hasMissingRequiredComponent ) {
93+ return false ;
94+ }
95+
8996 if ( ! projectId . match ( / ^ \d + $ / ) ) {
90- throw new SentryError ( `Invalid Sentry Dsn: Invalid projectId ${ projectId } ` ) ;
97+ logger . error ( `Invalid Sentry Dsn: Invalid projectId ${ projectId } ` ) ;
98+ return false ;
9199 }
92100
93101 if ( ! isValidProtocol ( protocol ) ) {
94- throw new SentryError ( `Invalid Sentry Dsn: Invalid protocol ${ protocol } ` ) ;
102+ logger . error ( `Invalid Sentry Dsn: Invalid protocol ${ protocol } ` ) ;
103+ return false ;
95104 }
96105
97106 if ( port && isNaN ( parseInt ( port , 10 ) ) ) {
98- throw new SentryError ( `Invalid Sentry Dsn: Invalid port ${ port } ` ) ;
107+ logger . error ( `Invalid Sentry Dsn: Invalid port ${ port } ` ) ;
108+ return false ;
99109 }
100110
101111 return true ;
@@ -106,14 +116,9 @@ function validateDsn(dsn: DsnComponents): boolean | void {
106116 * @returns a valid DsnComponents object or `undefined` if @param from is an invalid DSN source
107117 */
108118export function makeDsn ( from : DsnLike ) : DsnComponents | undefined {
109- try {
110- const components = typeof from === 'string' ? dsnFromString ( from ) : dsnFromComponents ( from ) ;
111- validateDsn ( components ) ;
112- return components ;
113- } catch ( e ) {
114- if ( e instanceof SentryError ) {
115- logger . error ( e . message ) ;
116- }
119+ const components = typeof from === 'string' ? dsnFromString ( from ) : dsnFromComponents ( from ) ;
120+ if ( ! components || ! validateDsn ( components ) ) {
117121 return undefined ;
118122 }
123+ return components ;
119124}
0 commit comments