Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

requests.get.stream and 404 handling #95

Closed
mr-cloud opened this issue Sep 12, 2022 · 5 comments
Closed

requests.get.stream and 404 handling #95

mr-cloud opened this issue Sep 12, 2022 · 5 comments

Comments

@mr-cloud
Copy link
Owner

Hi, I'm feeling I'm doing something wrong but there seems no other way than to catch RequestFailedException.

I have:

requests.get.stream(url, params = parameters, headers = headers
    , onHeadersReceived = streamHeaders => {
  if (streamHeaders.statusCode == HttpServletResponse.SC_OK) {
    // Do stuff
  } else {
    // other stuff
  }
}).readBytesThrough{ inputStream =>
  // Only want to process if statusCode == 200
}

The only way to safely process this is to surround with try-catch:

try {
  requests.get.stream(url, params = parameters, headers = headers
    , onHeadersReceived = streamHeaders => {
    if (streamHeaders.statusCode == HttpServletResponse.SC_OK) {
      // Do stuff
    } else {
      // other stuff
    }
  }).readBytesThrough{ inputStream =>
    // Only want to process if statusCode == 200
  }
} catch {
  catch e: RequestFailedException =>
    // handle
}

This doesn't seem very scala-ish, is there a better way?
ID: 50
Original Author: andreak
link: Original Link

@mr-cloud
Copy link
Owner Author

Questions go in the gitter channel
Original Author:lihaoyi

@mr-cloud
Copy link
Owner Author

#53 solved it, using "check = false"
I will ask on gitter next time.
Original Author:andreak

@mr-cloud
Copy link
Owner Author

There doesn't seem to be much activity on the gitter-channel. I haven't seen one question answered...
Original Author:andreak

@mr-cloud
Copy link
Owner Author

#53 solved it, using "check = false"
I will ask on gitter next time.

I'm having a very similar issue. I'm wondering how check = false solved it. I'm wondering what your final solution looked like?
Original Author:mikeaadd

@mr-cloud
Copy link
Owner Author

check=false solved it because then the library doesn't throw exception, but calls processWrappedStream.
Se the relevant code in Requester:

          if (streamHeaders.is2xx || !check) processWrappedStream(f)
          else {
            val errorOutput = new ByteArrayOutputStream()
            processWrappedStream(geny.Internal.transfer(_, errorOutput))
            throw new RequestFailedException(
              Response(
                streamHeaders.url,
                streamHeaders.statusCode,
                streamHeaders.statusMessage,
                new geny.Bytes(errorOutput.toByteArray),
                streamHeaders.headers,
                streamHeaders.history
              )
            )
          }

Original Author:andreak

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant