Skip to content

Commit 32c9456

Browse files
fix: URL validation and error message (#816)
for env var P_INGESTOR_ENDPOINT
1 parent 36aa929 commit 32c9456

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

server/src/handlers/airplane.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,8 @@ pub fn server() -> impl Future<Output = Result<(), Box<dyn std::error::Error + S
324324
.parseable
325325
.address
326326
.parse()
327-
.expect("valid socket address");
327+
.unwrap_or_else(|err| panic!("{}, failed to parse `{}` as a socket address. Please set the environment variable `P_ADDR` to `<ip address>:<port>` without the scheme (e.g., 192.168.1.1:8000). Please refer to the documentation: https://logg.ing/env for more details.",
328+
CONFIG.parseable.address, err));
328329
addr.set_port(CONFIG.parseable.flight_port);
329330

330331
let service = AirServiceImpl {};

server/src/utils.rs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,21 @@ pub fn get_url() -> Url {
238238
CONFIG.parseable.address
239239
)
240240
.parse::<Url>() // if the value was improperly set, this will panic before hand
241-
.expect("Valid URL");
241+
.unwrap_or_else(|err| panic!("{}, failed to parse `{}` as Url. Please set the environment variable `P_ADDR` to `<ip address>:<port>` without the scheme (e.g., 192.168.1.1:8000). Please refer to the documentation: https://logg.ing/env for more details.",
242+
err, CONFIG.parseable.address));
243+
}
244+
245+
let ingestor_endpoint = &CONFIG.parseable.ingestor_endpoint;
246+
247+
if ingestor_endpoint.starts_with("http") {
248+
panic!("Invalid value `{}`, please set the environement variable `P_INGESTOR_ENDPOINT` to `<ip address / DNS>:<port>` without the scheme (e.g., 192.168.1.1:8000 or example.com:8000). Please refer to the documentation: https://logg.ing/env for more details.", ingestor_endpoint);
249+
}
250+
251+
let addr_from_env = ingestor_endpoint.split(':').collect::<Vec<&str>>();
252+
253+
if addr_from_env.len() != 2 {
254+
panic!("Invalid value `{}`, please set the environement variable `P_INGESTOR_ENDPOINT` to `<ip address / DNS>:<port>` without the scheme (e.g., 192.168.1.1:8000 or example.com:8000). Please refer to the documentation: https://logg.ing/env for more details.", ingestor_endpoint);
242255
}
243-
let addr_from_env = CONFIG
244-
.parseable
245-
.ingestor_endpoint
246-
.split(':')
247-
.collect::<Vec<&str>>();
248256

249257
let mut hostname = addr_from_env[0].to_string();
250258
let mut port = addr_from_env[1].to_string();
@@ -254,16 +262,30 @@ pub fn get_url() -> Url {
254262
if hostname.starts_with('$') {
255263
let var_hostname = hostname[1..].to_string();
256264
hostname = get_from_env(&var_hostname);
257-
}
258-
if !hostname.starts_with("http") {
259-
hostname = format!("{}://{}", CONFIG.parseable.get_scheme(), hostname);
265+
266+
if hostname.is_empty() {
267+
panic!("The environement variable `{}` is not set, please set as <ip address / DNS> without the scheme (e.g., 192.168.1.1 or example.com). Please refer to the documentation: https://logg.ing/env for more details.", var_hostname);
268+
}
269+
if hostname.starts_with("http") {
270+
panic!("Invalid value `{}`, please set the environement variable `{}` to `<ip address / DNS>` without the scheme (e.g., 192.168.1.1 or example.com). Please refer to the documentation: https://logg.ing/env for more details.", hostname, var_hostname);
271+
} else {
272+
hostname = format!("{}://{}", CONFIG.parseable.get_scheme(), hostname);
273+
}
260274
}
261275

262276
if port.starts_with('$') {
263277
let var_port = port[1..].to_string();
264278
port = get_from_env(&var_port);
279+
280+
if port.is_empty() {
281+
panic!(
282+
"Port is not set in the environement variable `{}`. Please refer to the documentation: https://logg.ing/env for more details.",
283+
var_port
284+
);
285+
}
265286
}
266-
format!("{}:{}", hostname, port)
287+
288+
format!("{}://{}:{}", CONFIG.parseable.get_scheme(), hostname, port)
267289
.parse::<Url>()
268290
.expect("Valid URL")
269291
}

0 commit comments

Comments
 (0)