Skip to content

Conversation

@HirotoShioi
Copy link
Contributor

@HirotoShioi HirotoShioi commented Jul 9, 2018

This PR will introduce exception handling on reading log files so that the program will not crash due to the corrupted log file.

  • Fix test cases (Don't use external file when testing)
  • Exception handling on reading log files. (Example tciket id: 15066)

https://iohk.myjetbrains.com/youtrack/issue/TSD-70

@HirotoShioi HirotoShioi added the WIP label Jul 9, 2018
-- | Given number of file of inspect, knowledgebase and attachment,
-- analyze the logs and return the results.
inspectAttachment :: Config -> TicketInfo -> AttachmentContent -> ZendeskResponse
inspectAttachment :: (MonadCatch m) => Config -> TicketInfo -> AttachmentContent -> m ZendeskResponse
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't touch this, this is all pure, no need to infect that part of the code with impurities.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it!

src/Lib.hs Outdated
att <- MaybeT $ getAttachment lastAttachment

pure $ inspectAttachment config ticketInfo att
catchAny (inspectAttachment config ticketInfo att)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not looking for Any exception, we need specific exceptions we can throw and catch. Like here - https://github.com/input-output-hk/cardano-sl/blob/feature/CBR-163/wallet-new/src/Cardano/Wallet/WalletLayer/Error.hs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the advice! Will look into it!

@HirotoShioi
Copy link
Contributor Author

Implemented obvious ones. Now I'm going to try to solve exception *** Exception: Codec.Compression.Zlib: premature end of compressed data stream

@HirotoShioi HirotoShioi changed the title [TSD-79] Exception handling when reading log files [TSD-70] Exception handling when reading log files Jul 10, 2018
@HirotoShioi HirotoShioi force-pushed the hiroto/TSD-79-exception-handling branch 2 times, most recently from 7b41e15 to 54d39b7 Compare July 11, 2018 05:07
src/Lib.hs Outdated
-- analyze the logs and return the results.
inspectAttachment :: Config -> TicketInfo -> AttachmentContent -> ZendeskResponse
inspectAttachment Config{..} ticketInfo@TicketInfo{..} attContent = do
inspectAttachment :: Config -> TicketInfo -> [LByteString]-> ZendeskResponse
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you strip the newtype wrapper?

Copy link
Contributor Author

@HirotoShioi HirotoShioi Jul 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, previously, we were passing raw zip file asAttachmentContent whereas I've changed it to pass the decompressed log files.
The only way I found of making this function pure is to decompress the file before passing it instead of decompressing it inside of this function.

, MonadReader Config
, MonadIO
, MonadBase IO
, MonadCatch
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really think we don't need this, unless you want to re-export, but that's a whole different story.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry, I thought that was from the module exports 😁
Should have looked in more detail.

, optparse-applicative
, reflection
, regex-tdfa
, safe-exceptions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@HirotoShioi HirotoShioi Jul 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried importing tryDeep from Universum.Exceptions but didn't work. I'm not sure what they mean by re-export. Perhaps it's because we're using the older version of universum (1.1.0)
cardano-sl 's wallet-new component is importing safe-exceptions even though it's using universum as well.
https://github.com/input-output-hk/cardano-sl/blob/develop/wallet-new/cardano-sl-wallet-new.cabal#L175

Maybe there's some way to import it and I just don't know how.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, keep it there, just try using functions from Universum as much as possible.

src/Lib.hs Outdated
postTicketComment <- asksZendeskLayer zlPostTicketComment


-- What should we do if the zendeskResponse was Nothing?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point of this refactoring is to add error details and remove Maybe where possible. Maybe you would have an idea what to do with it if you had specific exceptions you could catch here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay, I'd tackle on that part once I'm done with handling the decompression issue..

-- Why tryDeep instead of try? It's because we need to fully evaluate bytestrings in order to catch
-- the decompression issue.
readZip :: (MonadCatch m, MonadIO m) => LByteString -> m (Map FilePath LByteString)
readZip rawzip = case Zip.toArchiveOrFail rawzip of
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try it out? Does it catch the exceptions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I did try to use try at first since it has less constraints but it didn't catch the decompression error.
I can try again.

@HirotoShioi HirotoShioi force-pushed the hiroto/TSD-79-exception-handling branch 2 times, most recently from 63d68e1 to 8a6cb8f Compare July 17, 2018 07:56
coComment :: Comment
}

newtype LogFiles = LogFiles {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the formatting seen here - https://github.com/input-output-hk/log-classifier/pull/26/files#diff-ee661ebeb72d3f09577f0fd782917c5aR359 since it doesn't change any other lines besides the ones you change which is helpful.

-- Check we have some comments.
assert $ isLeft (eZendeskResponse :: Either ProcessTicketExceptions ZendeskResponse)
whenLeft eZendeskResponse $ \processException ->
assert $ processException == TicketInfoNotFound ticketId
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

src/Lib.hs Outdated
att <- MaybeT $ getAttachment lastAttachment

pure $ inspectAttachment config ticketInfo att
mAtt <- runMaybeT $ do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems overly verbose. Can you move it around a bit to make it a bit more concise?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try it out. Thank you.

-- Check it throws exception
-- How do I check if it returns proper exception?
assert $ isLeft (eZendeskResponse :: Either ProcessTicketExceptions ZendeskResponse)
whenLeft eZendeskResponse $ \processException ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work.

@HirotoShioi HirotoShioi removed the WIP label Jul 17, 2018
@HirotoShioi HirotoShioi force-pushed the hiroto/TSD-79-exception-handling branch 3 times, most recently from 3e0add1 to f219ec7 Compare July 18, 2018 06:37
@HirotoShioi HirotoShioi force-pushed the hiroto/TSD-79-exception-handling branch from e0f0ae4 to f179118 Compare July 19, 2018 09:32
Copy link
Contributor

@ksaric ksaric left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

src/Lib.hs Outdated
att <- MaybeT $ getAttachment lastAttachment

pure $ inspectAttachment config ticketInfo att
lastAttach <- handleMaybe (safeHead . reverse . sort $ attachments)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handleMaybe . safeHead . reverse . sort $ attachments

test/Spec.hs Outdated

-- Check we have some comments.
assert $ isJust zendeskResponse
assert $ (not . null . zrComment) zendeskResponse
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert . not . null . zrComment $ zendeskResponse

Copy link
Contributor

@mdimjasevic mdimjasevic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far I finishing reading everything but the tests, which looks good. I'll finish with tests tomorrow.

src/Lib.hs Outdated
-- No attachment, no comments means something is wrong with ticket itself

-- | Inspect only the latest attachment. We could propagate this
-- @Maybe@ upwards or use an @Either@ which will go hand in hand
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the documentation of the inspectAttachments function be updated now that the return type is not App (Maybe ZendeskResponse) anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I'll update it.

@HirotoShioi HirotoShioi force-pushed the hiroto/TSD-79-exception-handling branch from 27c6092 to 5a7719a Compare July 20, 2018 03:13
Copy link
Contributor

@mdimjasevic mdimjasevic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I with the test/Spec.hs was better organised: it looks like there is a lot of repetition of very similar code.

Other than that, this looks good to me.

@HirotoShioi HirotoShioi merged commit b1feef4 into develop Jul 20, 2018
@HirotoShioi HirotoShioi deleted the hiroto/TSD-79-exception-handling branch August 2, 2018 04:09
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

Successfully merging this pull request may close these issues.

4 participants