From 0b85684168dcd25a1006ba670ff1d0ca5ca0de98 Mon Sep 17 00:00:00 2001 From: Chaim Lev-Ari Date: Fri, 11 Jun 2021 03:05:54 +0300 Subject: [PATCH] fix(app): parse response with null body (#4654) * fix(app): parse response with null body * style(docker): add comment explaining change * fix(images): show correct error when failing import * fix(images): use async await --- app/docker/rest/response/handlers.js | 4 + .../images/build/buildImageController.js | 170 +++++++++--------- .../images/import/importImageController.js | 2 +- 3 files changed, 88 insertions(+), 88 deletions(-) diff --git a/app/docker/rest/response/handlers.js b/app/docker/rest/response/handlers.js index 7e14da023..4eb6e370f 100644 --- a/app/docker/rest/response/handlers.js +++ b/app/docker/rest/response/handlers.js @@ -18,6 +18,10 @@ function isJSON(jsonString) { // This handler wrap the JSON objects in an array. // Used by the API in: Image push, Image create, Events query. export function jsonObjectsToArrayHandler(data) { + // catching empty data helps the function not to fail and prevents unwanted error message to user. + if (!data) { + return []; + } var str = '[' + data.replace(/\n/g, ' ').replace(/\}\s*\{/g, '}, {') + ']'; return angular.fromJson(str); } diff --git a/app/docker/views/images/build/buildImageController.js b/app/docker/views/images/build/buildImageController.js index 5b7ddd23c..8974f915b 100644 --- a/app/docker/views/images/build/buildImageController.js +++ b/app/docker/views/images/build/buildImageController.js @@ -1,58 +1,56 @@ -angular.module('portainer.docker').controller('BuildImageController', [ - '$scope', - '$window', - 'ModalService', - 'BuildService', - 'Notifications', - 'HttpRequestHelper', - function ($scope, $window, ModalService, BuildService, Notifications, HttpRequestHelper) { - $scope.state = { - BuildType: 'editor', - actionInProgress: false, - activeTab: 0, - isEditorDirty: false, - }; +angular.module('portainer.docker').controller('BuildImageController', BuildImageController); - $scope.formValues = { - ImageNames: [{ Name: '' }], - UploadFile: null, - DockerFileContent: '', - URL: '', - Path: 'Dockerfile', - NodeName: null, - }; +function BuildImageController($scope, $async, $window, ModalService, BuildService, Notifications, HttpRequestHelper) { + $scope.state = { + BuildType: 'editor', + actionInProgress: false, + activeTab: 0, + isEditorDirty: false, + }; - $window.onbeforeunload = () => { - if ($scope.state.BuildType === 'editor' && $scope.formValues.DockerFileContent && $scope.state.isEditorDirty) { - return ''; - } - }; + $scope.formValues = { + ImageNames: [{ Name: '' }], + UploadFile: null, + DockerFileContent: '', + URL: '', + Path: 'Dockerfile', + NodeName: null, + }; - $scope.addImageName = function () { - $scope.formValues.ImageNames.push({ Name: '' }); - }; - - $scope.removeImageName = function (index) { - $scope.formValues.ImageNames.splice(index, 1); - }; - - function buildImageBasedOnBuildType(method, names) { - var buildType = $scope.state.BuildType; - var dockerfilePath = $scope.formValues.Path; - - if (buildType === 'upload') { - var file = $scope.formValues.UploadFile; - return BuildService.buildImageFromUpload(names, file, dockerfilePath); - } else if (buildType === 'url') { - var URL = $scope.formValues.URL; - return BuildService.buildImageFromURL(names, URL, dockerfilePath); - } else { - var dockerfileContent = $scope.formValues.DockerFileContent; - return BuildService.buildImageFromDockerfileContent(names, dockerfileContent); - } + $window.onbeforeunload = () => { + if ($scope.state.BuildType === 'editor' && $scope.formValues.DockerFileContent && $scope.state.isEditorDirty) { + return ''; } + }; - $scope.buildImage = function () { + $scope.addImageName = function () { + $scope.formValues.ImageNames.push({ Name: '' }); + }; + + $scope.removeImageName = function (index) { + $scope.formValues.ImageNames.splice(index, 1); + }; + + function buildImageBasedOnBuildType(method, names) { + var buildType = $scope.state.BuildType; + var dockerfilePath = $scope.formValues.Path; + + if (buildType === 'upload') { + var file = $scope.formValues.UploadFile; + return BuildService.buildImageFromUpload(names, file, dockerfilePath); + } else if (buildType === 'url') { + var URL = $scope.formValues.URL; + return BuildService.buildImageFromURL(names, URL, dockerfilePath); + } else { + var dockerfileContent = $scope.formValues.DockerFileContent; + return BuildService.buildImageFromDockerfileContent(names, dockerfileContent); + } + } + + $scope.buildImage = buildImage; + + async function buildImage() { + return $async(async () => { var buildType = $scope.state.BuildType; if (buildType === 'editor' && $scope.formValues.DockerFileContent === '') { @@ -71,44 +69,42 @@ angular.module('portainer.docker').controller('BuildImageController', [ var nodeName = $scope.formValues.NodeName; HttpRequestHelper.setPortainerAgentTargetHeader(nodeName); - buildImageBasedOnBuildType(buildType, imageNames) - .then(function success(data) { - $scope.buildLogs = data.buildLogs; - $scope.state.activeTab = 1; - if (data.hasError) { - Notifications.error('An error occured during build', { msg: 'Please check build logs output' }); - } else { - Notifications.success('Image successfully built'); - $scope.state.isEditorDirty = false; - } - }) - .catch(function error(err) { - Notifications.error('Failure', err, 'Unable to build image'); - }) - .finally(function final() { - $scope.state.actionInProgress = false; - }); - }; - - $scope.validImageNames = function () { - for (var i = 0; i < $scope.formValues.ImageNames.length; i++) { - var item = $scope.formValues.ImageNames[i]; - if (item.Name !== '') { - return true; + try { + const data = await buildImageBasedOnBuildType(buildType, imageNames); + $scope.buildLogs = data.buildLogs; + $scope.state.activeTab = 1; + if (data.hasError) { + Notifications.error('An error occurred during build', { msg: 'Please check build logs output' }); + } else { + Notifications.success('Image successfully built'); + $scope.state.isEditorDirty = false; } + } catch (err) { + Notifications.error('Failure', err, 'Unable to build image'); + } finally { + $scope.state.actionInProgress = false; } - return false; - }; + }); + } - $scope.editorUpdate = function (cm) { - $scope.formValues.DockerFileContent = cm.getValue(); - $scope.state.isEditorDirty = true; - }; - - this.uiCanExit = async function () { - if ($scope.state.BuildType === 'editor' && $scope.formValues.DockerFileContent && $scope.state.isEditorDirty) { - return ModalService.confirmWebEditorDiscard(); + $scope.validImageNames = function () { + for (var i = 0; i < $scope.formValues.ImageNames.length; i++) { + var item = $scope.formValues.ImageNames[i]; + if (item.Name !== '') { + return true; } - }; - }, -]); + } + return false; + }; + + $scope.editorUpdate = function (cm) { + $scope.formValues.DockerFileContent = cm.getValue(); + $scope.state.isEditorDirty = true; + }; + + this.uiCanExit = async function () { + if ($scope.state.BuildType === 'editor' && $scope.formValues.DockerFileContent && $scope.state.isEditorDirty) { + return ModalService.confirmWebEditorDiscard(); + } + }; +} diff --git a/app/docker/views/images/import/importImageController.js b/app/docker/views/images/import/importImageController.js index d8d7d0027..54b1a4838 100644 --- a/app/docker/views/images/import/importImageController.js +++ b/app/docker/views/images/import/importImageController.js @@ -25,7 +25,7 @@ angular.module('portainer.docker').controller('ImportImageController', [ Notifications.success('Images successfully uploaded'); }) .catch(function error(err) { - Notifications.error('Failure', err.message, 'Unable to upload image'); + Notifications.error('Failure', err, 'Unable to upload image'); }) .finally(function final() { $scope.state.actionInProgress = false;