forked from metabase/metabase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add context to the IOException when creating temp files
When sending an email with an attachment, we write the attachment as a temp file. This uses the `java.io.File/createTempFile` method. If for some reason we're not able to write that file, maybe because we don't have sufficient permissions, that method will throw an IOException. The IOException doesn't include the path of the file it was trying to create, which makes it difficult to diagnose and fix. This commit catches that original IOException and rethrows it with the path of the temp directory that is being used. Fixes metabase#8405
- Loading branch information
Showing
2 changed files
with
39 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
(ns metabase.email.messages-test | ||
(:require [expectations :refer :all] | ||
[metabase.email-test :refer [inbox with-fake-inbox]] | ||
[metabase.email.messages :refer [send-new-user-email! send-password-reset-email!]])) | ||
[metabase.email.messages :as msgs :refer [send-new-user-email! send-password-reset-email!]] | ||
[metabase.test.util :as tu]) | ||
(:import java.io.IOException)) | ||
|
||
;; new user email | ||
;; NOTE: we are not validating the content of the email body namely because it's got randomized elements and thus | ||
|
@@ -28,3 +30,23 @@ | |
(send-password-reset-email! "[email protected]" (not :google-auth) "test.domain.com" "http://localhost/some/url") | ||
(-> (@inbox "[email protected]") | ||
(update-in [0 :body 0] dissoc :content)))) | ||
|
||
(defmacro ^:private with-create-temp-failure [& body] | ||
`(with-redefs [msgs/create-temp-file (fn [_#] | ||
(throw (IOException. "Failed to write file")))] | ||
~@body)) | ||
|
||
;; Test that IOException bubbles up | ||
(expect | ||
IOException | ||
(with-create-temp-failure | ||
(#'msgs/create-temp-file-or-throw "txt"))) | ||
|
||
;; When we fail to create the temp file, include the directory in the error message | ||
(expect | ||
(re-pattern (format "Unable to create temp file in `%s`" (System/getProperty "java.io.tmpdir"))) | ||
(try | ||
(with-create-temp-failure | ||
(#'msgs/create-temp-file-or-throw "txt")) | ||
(catch Exception e | ||
(.getMessage e)))) |