summaryrefslogtreecommitdiff
path: root/cli/npm/semver/errors.rs
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2022-09-01 11:50:12 -0400
committerGitHub <noreply@github.com>2022-09-01 11:50:12 -0400
commite1d7d7b0e365c3ddfee9e52371dd2c6d75e132e6 (patch)
tree526dc1bfb9fd1b4e491c8a06cacd954398356d48 /cli/npm/semver/errors.rs
parent20c835407c695a0cc92be45604bbe5ea652565ea (diff)
fix(npm): better node version and version requirement compatibility (#15714)
Diffstat (limited to 'cli/npm/semver/errors.rs')
-rw-r--r--cli/npm/semver/errors.rs38
1 files changed, 38 insertions, 0 deletions
diff --git a/cli/npm/semver/errors.rs b/cli/npm/semver/errors.rs
new file mode 100644
index 000000000..530d73c55
--- /dev/null
+++ b/cli/npm/semver/errors.rs
@@ -0,0 +1,38 @@
+// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
+
+use deno_core::anyhow::bail;
+use deno_core::error::AnyError;
+use monch::ParseError;
+use monch::ParseErrorFailure;
+use monch::ParseResult;
+
+pub fn with_failure_handling<'a, T>(
+ combinator: impl Fn(&'a str) -> ParseResult<T>,
+) -> impl Fn(&'a str) -> Result<T, AnyError> {
+ move |input| match combinator(input) {
+ Ok((input, result)) => {
+ if !input.is_empty() {
+ error_for_failure(fail_for_trailing_input(input))
+ } else {
+ Ok(result)
+ }
+ }
+ Err(ParseError::Backtrace) => {
+ error_for_failure(fail_for_trailing_input(input))
+ }
+ Err(ParseError::Failure(e)) => error_for_failure(e),
+ }
+}
+
+fn error_for_failure<T>(e: ParseErrorFailure) -> Result<T, AnyError> {
+ bail!(
+ "{}\n {}\n ~",
+ e.message,
+ // truncate the output to prevent wrapping in the console
+ e.input.chars().take(60).collect::<String>()
+ )
+}
+
+fn fail_for_trailing_input(input: &str) -> ParseErrorFailure {
+ ParseErrorFailure::new(input, "Unexpected character.")
+}