func getApplicationErrorMessage()

in whisk/client.go [623:702]


func getApplicationErrorMessage(errResp interface{}) string {
	var errStr string

	// Handle error results that looks like:
	//
	//   {
	//     "error": {
	//       "error": "An error string",
	//       "message": "An error message",
	//       "another-message": "Another error message"
	//     }
	//   }
	//   Returns "An error string; An error message; Another error message"
	//
	// OR
	//   {
	//     "error": "An error string"
	//   }
	//   Returns "An error string"
	//
	// OR
	//   {
	//     "error": {
	//       "custom-err": {
	//         "error": "An error string",
	//         "message": "An error message"
	//       }
	//     }
	//   }
	//   Returns "{"error": { "custom-err": { "error": "An error string", "message": "An error message" } } }"

	errMapIntf, errMapIntfOk := errResp.(map[string]interface{})
	if !errMapIntfOk {
		errStr = fmt.Sprintf("%v", errResp)
	} else {
		// Check if the "error" field in the response JSON
		errObjIntf, errObjIntfOk := errMapIntf["error"]
		if !errObjIntfOk {
			errStr = fmt.Sprintf("%v", errMapIntf)
		} else {
			// Check if the "error" field value is a JSON object
			errObj, errObjOk := errObjIntf.(map[string]interface{})
			if !errObjOk {
				// The "error" field value is not JSON; check if it's a string
				errorStr, errorStrOk := errObjIntf.(string)
				if !errorStrOk {
					errStr = fmt.Sprintf("%v", errObjIntf)
				} else {
					errStr = errorStr
				}
			} else {
				Debug(DbgInfo, "Application failure error json: %+v\n", errObj)

				// Concatenate all string field values into a single error string
				// Note: entry order of strings from map are not preserved.
				msgSeparator := ""
				for _, val := range errObj {
					valStr, valStrOk := val.(string)
					if valStrOk {
						errStr = errStr + msgSeparator + valStr
						msgSeparator = "; "
					}
				}

				// If no top level string fields exist, return the entire error object
				// Return a nice JSON string if possible; otherwise let Go try it's best
				if len(errStr) == 0 {
					jsonBytes, err := json.Marshal(errObj)
					if err != nil {
						errStr = fmt.Sprintf("%v", errObj)
					} else {
						errStr = string(jsonBytes)
					}
				}
			}
		}
	}

	return errStr
}