Package Kit: Return proper errors in FetchFileJob

This should give more meaningful errors in pkgman
(not so sure, feel free to tweak the mapping),
and also avoid running the next job if fetching failed.

Change-Id: If76968f705ff25f7ecf1a5f91d88a02040d24665
Reviewed-on: https://review.haiku-os.org/c/haiku/+/2186
Reviewed-by: Ryan Leavengood <leavengood@gmail.com>
This commit is contained in:
François Revol 2020-02-03 16:07:57 +01:00 committed by Ryan Leavengood
parent 8769583f90
commit 0ef0921d99
2 changed files with 56 additions and 39 deletions

View File

@ -17,6 +17,7 @@
#include <Path.h>
#ifdef HAIKU_TARGET_PLATFORM_HAIKU
# include <HttpRequest.h>
# include <UrlRequest.h>
# include <UrlProtocolRoster.h>
#endif
@ -36,6 +37,7 @@ FetchFileJob::FetchFileJob(const BContext& context, const BString& title,
fFileURL(fileURL),
fTargetEntry(targetEntry),
fTargetFile(&targetEntry, B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY),
fError(B_ERROR),
fDownloadProgress(0.0)
{
}
@ -96,41 +98,7 @@ FetchFileJob::Execute()
thread_id thread = request->Run();
wait_for_thread(thread, NULL);
if (fSuccess == true)
return B_OK;
else
return B_ERROR;
//TODO: More detailed error codes?
#if 0
const BHttpResult& outResult = dynamic_cast<const BHttpResult&>
(request->Result());
switch (outResult.StatusCode()) {
case B_HTTP_STATUS_OK:
return B_OK;
case B_HTTP_STATUS_PARTIAL_CONTENT:
return B_PARTIAL_READ;
case B_HTTP_STATUS_REQUEST_TIMEOUT:
case B_HTTP_STATUS_GATEWAY_TIMEOUT:
return B_TIMED_OUT;
case B_HTTP_STATUS_NOT_IMPLEMENTED:
case B_HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE:
return B_NOT_SUPPORTED;
case B_HTTP_STATUS_UNAUTHORIZED:
return B_PERMISSION_DENIED;
case B_HTTP_STATUS_FORBIDDEN:
case B_HTTP_STATUS_METHOD_NOT_ALLOWED:
case B_HTTP_STATUS_NOT_ACCEPTABLE:
return B_NOT_ALLOWED;
case B_HTTP_STATUS_NOT_FOUND:
return B_NAME_NOT_FOUND;
case B_HTTP_STATUS_BAD_GATEWAY:
return B_BAD_DATA;
default:
return B_ERROR;
}
#endif
return fError;
}
@ -156,9 +124,58 @@ FetchFileJob::DownloadProgress(BUrlRequest*, ssize_t bytesReceived,
void
FetchFileJob::RequestCompleted(BUrlRequest*, bool success)
FetchFileJob::RequestCompleted(BUrlRequest* request, bool success)
{
fSuccess = success;
fError = request->Status();
if (success) {
const BHttpResult& outResult = dynamic_cast<const BHttpResult&>
(request->Result());
uint16 code = outResult.StatusCode();
uint16 codeClass = BHttpRequest::StatusCodeClass(code);
switch (codeClass) {
case B_HTTP_STATUS_CLASS_CLIENT_ERROR:
case B_HTTP_STATUS_CLASS_SERVER_ERROR:
fError = B_IO_ERROR;
break;
default:
fError = B_OK;
break;
}
switch (code) {
case B_HTTP_STATUS_OK:
fError = B_OK;
break;
case B_HTTP_STATUS_PARTIAL_CONTENT:
fError = B_PARTIAL_READ;
break;
case B_HTTP_STATUS_REQUEST_TIMEOUT:
case B_HTTP_STATUS_GATEWAY_TIMEOUT:
fError = B_DEV_TIMEOUT;
break;
case B_HTTP_STATUS_NOT_IMPLEMENTED:
case B_HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE:
fError = B_NOT_SUPPORTED;
break;
case B_HTTP_STATUS_UNAUTHORIZED:
fError = B_PERMISSION_DENIED;
break;
case B_HTTP_STATUS_FORBIDDEN:
case B_HTTP_STATUS_METHOD_NOT_ALLOWED:
case B_HTTP_STATUS_NOT_ACCEPTABLE:
fError = B_NOT_ALLOWED;
break;
case B_HTTP_STATUS_NOT_FOUND:
fError = B_NAME_NOT_FOUND;
break;
case B_HTTP_STATUS_BAD_GATEWAY:
fError = B_BAD_DATA;
break;
default:
break;
}
}
}

View File

@ -50,7 +50,7 @@ public:
off_t position, ssize_t size);
virtual void DownloadProgress(BUrlRequest*, ssize_t bytesReceived,
ssize_t bytesTotal);
virtual void RequestCompleted(BUrlRequest*, bool success);
virtual void RequestCompleted(BUrlRequest* request, bool success);
#endif
protected:
@ -61,7 +61,7 @@ private:
BString fFileURL;
BEntry fTargetEntry;
BFile fTargetFile;
bool fSuccess;
status_t fError;
float fDownloadProgress;
off_t fBytes;
off_t fTotalBytes;