1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-04 04:07:16 +02:00

piperExecuteBin - prevent non-speaking exit code 1 error (#1584)

* piperExecuteBin - prevent non-speaking exit code 1 error

Make sure that error during context resolution gets more context.

* properly provide golang errors

* improve error message, when no details are available

* Add error category

* Update getConfig.go
This commit is contained in:
Oliver Nocon 2020-05-26 07:58:03 +02:00 committed by GitHub
parent 80f70aba4a
commit d5d510719d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 8 deletions

View File

@ -6,6 +6,7 @@ import (
"os"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
@ -28,8 +29,16 @@ func ConfigCommand() *cobra.Command {
var createConfigCmd = &cobra.Command{
Use: "getConfig",
Short: "Loads the project 'Piper' configuration respecting defaults and parameters.",
RunE: func(cmd *cobra.Command, _ []string) error {
return generateConfig()
PreRun: func(cmd *cobra.Command, args []string) {
path, _ := os.Getwd()
fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
log.RegisterHook(fatalHook)
},
Run: func(cmd *cobra.Command, _ []string) {
err := generateConfig()
if err != nil {
log.Entry().WithField("category", "config").WithError(err).Fatal("failed to retrieve configuration")
}
},
}

View File

@ -54,8 +54,7 @@ func TestConfigCommand(t *testing.T) {
t.Run("Run", func(t *testing.T) {
t.Run("Success case", func(t *testing.T) {
configOptions.openFile = configOpenFileMock
err := cmd.RunE(cmd, []string{})
assert.NoError(t, err, "error occured but none expected")
cmd.Run(cmd, []string{})
})
})
}

View File

@ -315,7 +315,7 @@ class PiperExecuteBinTest extends BasePiperTest {
helper.registerAllowedMethod('sh', [String.class], {s -> throw new AbortException('exit code 1')})
exception.expect(AbortException)
exception.expectMessage("[noDetailsStep] Step execution failed. Error: hudson.AbortException: exit code 1")
exception.expectMessage("[noDetailsStep] Step execution failed. Error: hudson.AbortException: exit code 1, please see log file for more details.")
stepRule.step.piperExecuteBin(
[

View File

@ -58,8 +58,11 @@ void call(Map parameters = [:], stepName, metadataFile, List credentialInfo, fai
echo "PIPER_parametersJSON: ${groovy.json.JsonOutput.toJson(stepParameters)}"
// get context configuration
Map config = readJSON(text: sh(returnStdout: true, script: "${piperGoPath} getConfig --contextConfig --stepMetadata '.pipeline/tmp/${metadataFile}'${defaultConfigArgs}${customConfigArg}"))
echo "Context Config: ${config}"
Map config
handleErrorDetails(stepName) {
config = readJSON(text: sh(returnStdout: true, script: "${piperGoPath} getConfig --contextConfig --stepMetadata '.pipeline/tmp/${metadataFile}'${defaultConfigArgs}${customConfigArg}"))
echo "Context Config: ${config}"
}
dockerWrapper(script, config) {
handleErrorDetails(stepName) {
@ -170,6 +173,6 @@ void handleErrorDetails(String stepName, Closure body) {
}
error "[${stepName}] Step execution failed${errorCategory}. Error: ${errorDetails.error?:errorDetails.message}"
}
error "[${stepName}] Step execution failed. Error: ${ex}"
error "[${stepName}] Step execution failed. Error: ${ex}, please see log file for more details."
}
}