[JAVA-913] MongoClient throws NumberFormatException when parsing http:// address Created: 31/Jul/13  Updated: 30/Jan/15  Resolved: 12/Aug/14

Status: Closed
Project: Java Driver
Component/s: Error Handling
Affects Version/s: 2.11.2
Fix Version/s: 2.13.0, 3.0.0

Type: Improvement Priority: Minor - P4
Reporter: Gavin Price Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

import com.mongodb.MongoClient;
 
 
public class MongoTest {
	
	public static void main(String[] args) throws Exception {
		new MongoClient("http://something.com");
	}
 
}
 
Exception in thread "main" java.lang.NumberFormatException: For input string: "//something.com"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:481)
	at java.lang.Integer.parseInt(Integer.java:527)
	at com.mongodb.ServerAddress.<init>(ServerAddress.java:67)
	at com.mongodb.ServerAddress.<init>(ServerAddress.java:46)
	at com.mongodb.MongoClient.<init>(MongoClient.java:93)
	at MongoTest.main(MongoTest.java:7)

Expected behavior: throw a more relevant exception, or at least a MongoException.



 Comments   
Comment by Githook User [ 30/Jan/15 ]

Author:

{u'username': u'rozza', u'name': u'Ross Lawley', u'email': u'ross.lawley@gmail.com'}

Message: Fixed NumberFormat Exception when uri is missing the port

JAVA-913 JAVA-1357
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/0b2734a76536ecfdcbbba2b3a35496410ae87566

Comment by Githook User [ 12/Aug/14 ]

Author:

{u'username': u'neerajbhatt', u'name': u'neerajbhatt', u'email': u'neerajbhatt2000@gmail.com'}

Message: Fixing NumberFormatException when parsing http:// address

https://jira.mongodb.org/browse/JAVA-913 Fixed, When http:// address is
given to MongoClient, it now throws a MongoException as told by Jeff

Refs JAVA-1357
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/b844c523143fdd56466cd98e18af8d0da294f554

Comment by Githook User [ 12/Aug/14 ]

Author:

{u'username': u'rozza', u'name': u'Ross Lawley', u'email': u'ross.lawley@gmail.com'}

Message: Fixed NumberFormat Exception when uri is missing the port

JAVA-913 JAVA-1357
Branch: 3.0.x
https://github.com/mongodb/mongo-java-driver/commit/0b2734a76536ecfdcbbba2b3a35496410ae87566

Comment by Githook User [ 12/Aug/14 ]

Author:

{u'username': u'neerajbhatt', u'name': u'neerajbhatt', u'email': u'neerajbhatt2000@gmail.com'}

Message: Fixing NumberFormatException when parsing http:// address

https://jira.mongodb.org/browse/JAVA-913 Fixed, When http:// address is
given to MongoClient, it now throws a MongoException as told by Jeff

Refs JAVA-1357
Branch: 2.13.x
https://github.com/mongodb/mongo-java-driver/commit/a965d3900d3730c84bdb23b138439af6ab4215e9

Comment by Neeraj Bhatt [ 12/Jul/14 ]

Should we comment out below code and just throw UnknownHostException or it is mandatory that constructor should be able to parse host:port format ?

int idx = host.indexOf( ":" );
if ( idx > 0 )

{ if ( port != defaultPort() ) throw new IllegalArgumentException( "can't specify port in construct and via host" ); port = Integer.parseInt( host.substring( idx + 1 ) ); host = host.substring( 0 , idx ).trim(); }
Comment by Neeraj Bhatt [ 20/Jun/14 ]

We need to change port = Integer.parseInt( host.substring( idx + 1 ) ); (line 70 of Server Addres) as you will always receive integer is not valid (host:port)

Generated at Thu Feb 08 08:53:22 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.