@@ -681,7 +681,15 @@ function PiwigoAPI.pwConnect(propertyTable)
681681 if (httpHeaders .status == 201 ) or (httpHeaders .status == 200 ) then
682682 -- successful connection to Piwigo
683683 -- Now check login result
684- local rtnBody = JSON :decode (httpResponse )
684+ -- Decode JSON safely
685+ local ok , rtnBody = pcall ( JSON .decode , JSON , httpResponse )
686+ if not ok or type (rtnBody ) ~= " table" then
687+ LrDialogs .message (
688+ " Cannot log in to Piwigo" ,
689+ " Invalid or unreadable server response"
690+ )
691+ return false
692+ end
685693 if rtnBody .stat == " ok" then
686694 -- login ok - store session cookies
687695 local cookies = {}
@@ -705,18 +713,28 @@ function PiwigoAPI.pwConnect(propertyTable)
705713 propertyTable .cookieHeader = table.concat (propertyTable .cookies ," ; " )
706714 propertyTable .Connected = true
707715 else
708- LrDialogs .message (" Cannot log in to Piwigo - " , rtnBody .err .. " , " .. rtnBody .message )
716+ LrDialogs .message (
717+ " Cannot log in to Piwigo" ,
718+ tostring (rtnBody .err or " Unknown error" )
719+ .. (rtnBody .message and (" , " .. rtnBody .message ) or " " )
720+ )
709721 return false
710722 end
711723 else
712- if httpHeaders .error then
713- statusDes = httpHeaders .error .name
714- status = httpHeaders .error .errorCode
724+ local statusCode , statusDesc
725+ status = httpHeaders and httpHeaders .status
726+ if httpHeaders and httpHeaders .error then
727+ statusCode = httpHeaders .error .errorCode or " ?"
728+ statusDesc = httpHeaders .error .name or " Unknown error"
715729 else
716- statusDes = httpHeaders . statusDes or httpHeaders . statusDesc
717- status = httpHeaders . status
730+ statusCode = status or " ? "
731+ statusDesc = ( httpHeaders and ( httpHeaders . statusDes or httpHeaders . statusDesc )) or " "
718732 end
719- LrDialogs .message (" Cannot log in to Piwigo - " , status .. " , " .. statusDes )
733+
734+ LrDialogs .message (
735+ " Cannot log in to Piwigo" ,
736+ tostring (statusCode ) .. (statusDesc ~= " " and (" , " .. statusDesc ) or " " )
737+ )
720738 return false
721739 end
722740
@@ -1696,8 +1714,7 @@ function PiwigoAPI.httpPostMultiPart(propertyTable, params)
16961714 end
16971715 if httpHeaders then
16981716 postHeaders .status = httpHeaders .status
1699- postHeaders .statusDesc = httpHeaders .statusDes or httpHeaders .statusDesc
1700-
1717+ postHeaders .statusDesc = (httpHeaders and (httpHeaders .statusDes or httpHeaders .statusDesc )) or " "
17011718 end
17021719 if not body then
17031720 postResponse .status = false
0 commit comments