Skip to content

Commit 56ce253

Browse files
authored
feat(java-shell): don't duplicate error message (#1104)
1 parent ab8dc28 commit 56ce253

File tree

5 files changed

+23
-4
lines changed

5 files changed

+23
-4
lines changed

packages/java-shell/src/main/kotlin/com/mongodb/mongosh/MongoShellConverter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ internal class MongoShellConverter(private val context: MongoShellContext, priva
9292
if (error.isHostObject && error.asHostObject<Any>() is Throwable) {
9393
future.completeExceptionally(error.asHostObject<Any>() as Throwable)
9494
} else {
95-
val message = error.toString() + (if (error.instanceOf(context, "Error")) "\n${error.getMember("stack").asString()}" else "")
95+
val message = if (error.instanceOf(context, "Error")) error.getMember("stack").asString() else error.toString()
9696
future.completeExceptionally(Exception(message))
9797
}
9898
})

packages/java-shell/src/test/kotlin/com/mongodb/mongosh/LiteralsTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class LiteralsTest : ShellTestCase() {
3232
@Test fun testObjectId() = test()
3333
@Test fun testString() = test()
3434
@Test fun testSymbol() = test()
35+
@Test fun testSyntaxError() = test()
3536
@Test fun testTimestamp() = test()
3637
@Test fun testUndefined() = test()
3738
@Test fun testUnknownSymbol() = test()

packages/java-shell/src/test/kotlin/com/mongodb/mongosh/util.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ fun doTest(testName: String, shell: MongoShell, testDataPath: String, db: String
5151
val checkResultClass = properties.any { (key, _) -> key == "checkResultClass" }
5252
val dontReplaceId = properties.any { (key, _) -> key == "dontReplaceId" }
5353
val dontCheckValue = properties.any { (key, _) -> key == "dontCheckValue" }
54-
val options = CompareOptions(checkResultClass, dontCheckValue, dontReplaceId, properties.mapNotNull { (key, value) ->
54+
val printStackTrace = properties.any { (key, _) -> key == "printStackTrace" }
55+
val options = CompareOptions(checkResultClass, dontCheckValue, dontReplaceId, printStackTrace, properties.mapNotNull { (key, value) ->
5556
when (key) {
5657
"getArrayItem" -> GetArrayItemCommand(value.toInt())
5758
"extractProperty" -> ExtractPropertyCommand(value)
@@ -89,7 +90,7 @@ fun doTest(testName: String, shell: MongoShell, testDataPath: String, db: String
8990
System.err.println("IGNORED:")
9091
e.printStackTrace()
9192
val message = e.message
92-
val msg = if (message != null && message.contains('\n')) message.substring(0, message.indexOf('\n')) else message
93+
val msg = if (message != null && message.contains('\n') && !cmd.options.printStackTrace) message.substring(0, message.indexOf('\n')) else message
9394
sb.append(e.javaClass.name).append(": ").append(msg?.trim())
9495
}
9596
}
@@ -152,7 +153,7 @@ private object AllTypesComparator : Comparator<Any?> {
152153
}
153154

154155
private class Command(val command: String, val options: CompareOptions)
155-
private class CompareOptions(val checkResultClass: Boolean, val dontCheckValue: Boolean, val dontReplaceId: Boolean, val commands: List<CompareCommand>)
156+
private class CompareOptions(val checkResultClass: Boolean, val dontCheckValue: Boolean, val dontReplaceId: Boolean, val printStackTrace: Boolean, val commands: List<CompareCommand>)
156157
private sealed class CompareCommand
157158
private class GetArrayItemCommand(val index: Int) : CompareCommand()
158159
private class ExtractPropertyCommand(val property: String) : CompareCommand()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
java.lang.Exception: SyntaxError: Missing semicolon. (1:6)
2+
3+
> 1 | select 1;
4+
| ^
5+
2 |
6+
at Parser._raise (all-standalone.js:18680:17)
7+
at Parser.raiseWithData (all-standalone.js:18673:17)
8+
at Parser.raise (all-standalone.js:18634:17)
9+
at Parser.semicolon (all-standalone.js:27791:10)
10+
at Parser.parseExpressionStatement (all-standalone.js:31019:10)
11+
at Parser.parseStatementContent (all-standalone.js:30608:19)
12+
at Parser.parseStatement (all-standalone.js:30472:17)
13+
at Parser.parseBlockOrModuleBlockBody (all-standalone.js:31061:25)
14+
at Parser.parseBlockBody (all-standalone.js:31052:10)
15+
at Parser.parseProgram (all-standalone.js:30393:10)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// command printStackTrace
2+
select 1;

0 commit comments

Comments
 (0)