I have a groovy script which run jenkins pipeline. I have used powershell script. The exception thrown by the powershell script is not being print the catch block. Below is my code:
def call() {
node {
timestamps {
ansiColor('xterm') {
try {
powershell """
throw new Exception("ERROR: This is a test Exception.")
"""
} catch (error) {
println("Caught error: ${error.getMessage()}")
}
}
}
}
}
I have also tried with error.message, error.printStackTrace(), error.cause.getMessage(). None of the printing the error message. Instead I got “script returned exit code 1”.
Can you help me capturing the message?
Hello,
The message script returned exit code 1
acctually is exception messsage.
I guess that you expected to see ERROR: This is a test Exception.
In that case you should use:
def call() {
node {
timestamps {
ansiColor('xterm') {
def statusCode = powershell(
script: """
throw new Exception("ERROR: This is a test Exception.")
""",
returnStatus: true
)
println("status code ${statusCode}")
}
}
}
}
def call() {
node {
def statusCode
timestamps {
ansiColor('xterm') {
try {
statusCode = powershell """
throw new Exception("ERROR: This is a test Exception.")
"""
} catch (error) {
println("status code ${statusCode}")
}
}
}
}
}
I tried this. This would print null in statusCode placeholder. @daotoan-hd
In this case, you need to use like this.
def call() {
node {
def stdout
timestamps {
ansiColor('xterm') {
try {
stdout= powershell (
script: """
throw new Exception("ERROR: This is a test Exception.")
""",
resturnStdout: true)
} catch (error) {
println("status code ${stdout}")
}
}
}
}
}