mirror of
https://github.com/heyarne/airsonic-ui.git
synced 2026-05-07 02:33:39 +02:00
Display api errors as notifications
This commit is contained in:
parent
7fd5e6189d
commit
aa04e68545
9 changed files with 118 additions and 61 deletions
|
|
@ -11,6 +11,12 @@
|
||||||
;; ::events/something-happening -> relevant to only this app
|
;; ::events/something-happening -> relevant to only this app
|
||||||
;; :single-colon/something -> coming from external sources (e.g. :audio/... or :routes/...) that are potentially reusable
|
;; :single-colon/something -> coming from external sources (e.g. :audio/... or :routes/...) that are potentially reusable
|
||||||
|
|
||||||
|
(re-frame/reg-fx
|
||||||
|
;; a simple effect to keep println statements out of our event handlers
|
||||||
|
:log
|
||||||
|
(fn [params]
|
||||||
|
(apply println params)))
|
||||||
|
|
||||||
;; database reset / init
|
;; database reset / init
|
||||||
|
|
||||||
(re-frame/reg-event-db
|
(re-frame/reg-event-db
|
||||||
|
|
@ -29,12 +35,24 @@
|
||||||
:http-xhrio {:method :get
|
:http-xhrio {:method :get
|
||||||
:uri (api/url server "ping" {:u user :p pass})
|
:uri (api/url server "ping" {:u user :p pass})
|
||||||
:response-format (ajax/json-response-format {:keywords? true})
|
:response-format (ajax/json-response-format {:keywords? true})
|
||||||
:on-success [::credentials-verified user pass]
|
:on-success [::verify-auth-response user pass]
|
||||||
:on-failure [::api-failure]}})
|
:on-failure [:api/bad-response]}})
|
||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
::authenticate authenticate)
|
::authenticate authenticate)
|
||||||
|
|
||||||
|
(defn verify-auth-response
|
||||||
|
"Since we don't get real status codes, we have to look into the server's
|
||||||
|
response and see whether we actually sent the correct credentials"
|
||||||
|
[fx [_ user pass response]]
|
||||||
|
{:dispatch (if (api/is-error? response)
|
||||||
|
[:notification/show :error (api/error-msg (api/->exception response))]
|
||||||
|
[::credentials-verified user pass])})
|
||||||
|
|
||||||
|
(re-frame/reg-event-fx
|
||||||
|
::verify-auth-response
|
||||||
|
verify-auth-response)
|
||||||
|
|
||||||
(defn try-remember-user
|
(defn try-remember-user
|
||||||
"Enables skipping the auth request when credentials are saved in the
|
"Enables skipping the auth request when credentials are saved in the
|
||||||
local storage; otherwise has no effect"
|
local storage; otherwise has no effect"
|
||||||
|
|
@ -51,7 +69,7 @@
|
||||||
(defn credentials-verified
|
(defn credentials-verified
|
||||||
"Gets called after the server indicates that the credentials entered by a user
|
"Gets called after the server indicates that the credentials entered by a user
|
||||||
are correct (see `authenticate`)"
|
are correct (see `authenticate`)"
|
||||||
[{:keys [db store]} [_event user pass _response]]
|
[{:keys [db store]} [_ user pass]]
|
||||||
(let [auth {:u user :p pass}
|
(let [auth {:u user :p pass}
|
||||||
credentials (merge (:credentials db) auth)]
|
credentials (merge (:credentials db) auth)]
|
||||||
{:routes/set-credentials auth
|
{:routes/set-credentials auth
|
||||||
|
|
@ -87,28 +105,32 @@
|
||||||
(let [creds (:credentials db)]
|
(let [creds (:credentials db)]
|
||||||
(api/url (:server creds) endpoint (merge params (select-keys creds [:u :p])))))
|
(api/url (:server creds) endpoint (merge params (select-keys creds [:u :p])))))
|
||||||
|
|
||||||
(defn api-request [{:keys [db]} [_ endpoint k params]]
|
(defn api-request [{:keys [db]} [_ endpoint params]]
|
||||||
{:http-xhrio {:method :get
|
{:http-xhrio {:method :get
|
||||||
:uri (api-url db endpoint params)
|
:uri (api-url db endpoint params)
|
||||||
:response-format (ajax/json-response-format {:keywords? true})
|
:response-format (ajax/json-response-format {:keywords? true})
|
||||||
:on-success [::api-success k]
|
:on-success [:api/good-response]
|
||||||
:on-failure [::api-failure]}})
|
:on-failure [:api/bad-response]}})
|
||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
:api-request
|
:api/request
|
||||||
api-request)
|
api-request)
|
||||||
|
|
||||||
(re-frame/reg-event-db
|
(defn good-api-response [fx [_ response]]
|
||||||
::api-success
|
(try
|
||||||
(fn [db [_ k response]]
|
(assoc-in fx [:db :response] (api/unwrap-response response))
|
||||||
; we "unwrap" the responses
|
(catch ExceptionInfo e
|
||||||
(assoc db :response (-> response :subsonic-response k))))
|
{:dispatch [:notification/show :error (api/error-msg e)]})))
|
||||||
|
|
||||||
(re-frame/reg-event-db
|
(re-frame/reg-event-fx
|
||||||
::api-failure
|
:api/good-response
|
||||||
|
good-api-response)
|
||||||
|
|
||||||
|
(re-frame/reg-event-fx
|
||||||
|
:api/bad-response
|
||||||
(fn [db event]
|
(fn [db event]
|
||||||
(println "api call gone bad; CORS headers missing? check for :status 0" event)
|
{:log ["API call gone bad; are CORS headers missing? check for :status 0" event]
|
||||||
db))
|
:dispatch [:notification/show :error "Communication with server failed. Check browser logs for details."]}))
|
||||||
|
|
||||||
;; musique
|
;; musique
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,16 +28,16 @@
|
||||||
|
|
||||||
(defmethod route-data ::main
|
(defmethod route-data ::main
|
||||||
[route-id params query]
|
[route-id params query]
|
||||||
[:api-request "getAlbumList2" :albumList2 {:type "recent"
|
[:api/request "getAlbumList2" {:type "recent"
|
||||||
:size 18}])
|
:size 18}])
|
||||||
|
|
||||||
(defmethod route-data ::artist-view
|
(defmethod route-data ::artist-view
|
||||||
[route-id params query]
|
[route-id params query]
|
||||||
[:api-request "getArtist" :artist (select-keys params [:id])])
|
[:api/request "getArtist" (select-keys params [:id])])
|
||||||
|
|
||||||
(defmethod route-data ::album-view
|
(defmethod route-data ::album-view
|
||||||
[route-id params query]
|
[route-id params query]
|
||||||
[:api-request "getAlbum" :album (select-keys params [:id])])
|
[:api/request "getAlbum" (select-keys params [:id])])
|
||||||
|
|
||||||
;; shouldn't need to change anything below
|
;; shouldn't need to change anything below
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,14 +26,27 @@
|
||||||
(defn is-error? [response]
|
(defn is-error? [response]
|
||||||
(= "failed" (get-in response [:subsonic-response :status])))
|
(= "failed" (get-in response [:subsonic-response :status])))
|
||||||
|
|
||||||
|
(defn- unwrap-response* [response]
|
||||||
|
(-> (:subsonic-response response)
|
||||||
|
(dissoc :status :version)
|
||||||
|
vals
|
||||||
|
first))
|
||||||
|
|
||||||
|
(defn ->exception
|
||||||
|
"Takes an erroneous response and makes it a real exception"
|
||||||
|
[response]
|
||||||
|
(let [error (unwrap-response* response)]
|
||||||
|
(ex-info (:message response) error)))
|
||||||
|
|
||||||
(defn unwrap-response
|
(defn unwrap-response
|
||||||
"Retrieves the actual response body"
|
"Retrieves the actual response body"
|
||||||
[response]
|
[response]
|
||||||
(if (is-error? response)
|
(if (is-error? response)
|
||||||
(let [error (:error response)]
|
(let [error (:error response)]
|
||||||
(throw (ex-info (:message response) error)))
|
(throw (->exception response)))
|
||||||
(-> (get-in response [:subsonic-response])
|
(unwrap-response* response)))
|
||||||
(dissoc :status :version)
|
|
||||||
vals
|
|
||||||
first)))
|
|
||||||
|
|
||||||
|
(defn error-msg
|
||||||
|
[exception-info]
|
||||||
|
(let [{:keys [code message]} (ex-data exception-info)]
|
||||||
|
(str "Error " code ": " message)))
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
[airsonic-ui.events :as events]
|
[airsonic-ui.events :as events]
|
||||||
[airsonic-ui.subs :as subs]
|
[airsonic-ui.subs :as subs]
|
||||||
|
|
||||||
|
[airsonic-ui.views.notifications :refer [notification-list]]
|
||||||
[airsonic-ui.views.breadcrumbs :refer [breadcrumbs]]
|
[airsonic-ui.views.breadcrumbs :refer [breadcrumbs]]
|
||||||
[airsonic-ui.views.bottom-bar :refer [bottom-bar]]
|
[airsonic-ui.views.bottom-bar :refer [bottom-bar]]
|
||||||
[airsonic-ui.views.login :refer [login-form]]
|
[airsonic-ui.views.login :refer [login-form]]
|
||||||
|
|
@ -46,26 +47,12 @@
|
||||||
{:on-click #(dispatch [::events/initialize-db]) :href "#"}
|
{:on-click #(dispatch [::events/initialize-db]) :href "#"}
|
||||||
(str "Logout (" (:name user) ")")]]]])
|
(str "Logout (" (:name user) ")")]]]])
|
||||||
|
|
||||||
;; user notifications
|
|
||||||
|
|
||||||
(defn notification-list [notifications]
|
|
||||||
[:div.notifications
|
|
||||||
(for [[id notification] notifications]
|
|
||||||
(let [class (case (:level notification)
|
|
||||||
:error "danger"
|
|
||||||
"info")]
|
|
||||||
^{:key id} [:div {:class-name (str "notification is-small is-" class)}
|
|
||||||
[:button.delete {:on-click #(dispatch [:notification/hide id])}]
|
|
||||||
(:message notification)]))])
|
|
||||||
|
|
||||||
;; putting everything together
|
;; putting everything together
|
||||||
|
|
||||||
(defn app [route params query]
|
(defn app [route params query]
|
||||||
(let [user @(subscribe [::subs/user])
|
(let [user @(subscribe [::subs/user])
|
||||||
notifications @(subscribe [::subs/notifications])
|
|
||||||
content @(subscribe [::subs/current-content])]
|
content @(subscribe [::subs/current-content])]
|
||||||
[:div
|
[:div
|
||||||
[notification-list notifications]
|
|
||||||
[:main.columns
|
[:main.columns
|
||||||
[:div.column.is-2.sidebar
|
[:div.column.is-2.sidebar
|
||||||
[sidebar user]]
|
[sidebar user]]
|
||||||
|
|
@ -79,7 +66,10 @@
|
||||||
[bottom-bar]]))
|
[bottom-bar]]))
|
||||||
|
|
||||||
(defn main-panel []
|
(defn main-panel []
|
||||||
(let [[route params query] @(subscribe [::subs/current-route])]
|
(let [[route params query] @(subscribe [::subs/current-route])
|
||||||
|
notifications @(subscribe [::subs/notifications])]
|
||||||
|
[:div
|
||||||
|
[notification-list notifications]
|
||||||
(case route
|
(case route
|
||||||
::routes/login [login-form]
|
::routes/login [login-form]
|
||||||
[app route params query])))
|
[app route params query])]))
|
||||||
|
|
|
||||||
14
src/cljs/airsonic_ui/views/notifications.cljs
Normal file
14
src/cljs/airsonic_ui/views/notifications.cljs
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
(ns airsonic-ui.views.notifications
|
||||||
|
(:require [re-frame.core :refer [dispatch]]))
|
||||||
|
|
||||||
|
;; user notifications
|
||||||
|
|
||||||
|
(defn notification-list [notifications]
|
||||||
|
[:div.notifications
|
||||||
|
(for [[id notification] notifications]
|
||||||
|
(let [class (case (:level notification)
|
||||||
|
:error "danger"
|
||||||
|
"info")]
|
||||||
|
^{:key id} [:div {:class-name (str "notification is-small is-" class)}
|
||||||
|
[:button.delete {:on-click #(dispatch [:notification/hide id])}]
|
||||||
|
(:message notification)]))])
|
||||||
|
|
@ -77,7 +77,8 @@
|
||||||
// floating notifications
|
// floating notifications
|
||||||
.notifications
|
.notifications
|
||||||
@extend .container
|
@extend .container
|
||||||
padding-top: 3.2rem
|
z-index: 100
|
||||||
position: fixed
|
position: fixed
|
||||||
left: 0
|
left: 0
|
||||||
right: 0
|
right: 0
|
||||||
|
padding-top: 3.2rem
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
(ns airsonic-ui.events-test
|
(ns airsonic-ui.events-test
|
||||||
(:require [cljs.test :refer [deftest testing is]]
|
(:require [cljs.test :refer [deftest testing is]]
|
||||||
[clojure.string :as str]
|
[clojure.string :as str]
|
||||||
|
[airsonic-ui.fixtures :refer [responses]]
|
||||||
[airsonic-ui.events :as events]))
|
[airsonic-ui.events :as events]))
|
||||||
|
|
||||||
(enable-console-print!)
|
(enable-console-print!)
|
||||||
|
|
@ -16,8 +17,14 @@
|
||||||
(testing "saves the given server location"
|
(testing "saves the given server location"
|
||||||
(is (= server (get-in fx [:db :credentials :server]))))
|
(is (= server (get-in fx [:db :credentials :server]))))
|
||||||
(testing "invokes correct success callback"
|
(testing "invokes correct success callback"
|
||||||
(is (= ::events/credentials-verified (first (:on-success request)))))))
|
(is (= ::events/verify-auth-response (first (:on-success request)))))))
|
||||||
(testing "On succesfull response"
|
(testing "Auth response verification"
|
||||||
|
(is (= :notification/show
|
||||||
|
(first (:dispatch (events/verify-auth-response {} [:_ "user" "pass" (:error responses)]))))
|
||||||
|
"shows an error when we have an error response")
|
||||||
|
(let [event (:dispatch (events/verify-auth-response {} [:_ "username" "password" (:auth-success responses)]))]
|
||||||
|
(is (= [::events/credentials-verified "username" "password"] event))))
|
||||||
|
(testing "On succesful response"
|
||||||
(let [creds-before {:server "https://localhost"}
|
(let [creds-before {:server "https://localhost"}
|
||||||
fx (events/credentials-verified {:db {:credentials creds-before}}
|
fx (events/credentials-verified {:db {:credentials creds-before}}
|
||||||
[:_ "user" "pass"])
|
[:_ "user" "pass"])
|
||||||
|
|
@ -47,6 +54,11 @@
|
||||||
(defn- first-notification [db]
|
(defn- first-notification [db]
|
||||||
(-> (:notifications db) vals first))
|
(-> (:notifications db) vals first))
|
||||||
|
|
||||||
|
(deftest api-interaction
|
||||||
|
(testing "Should show an error notification when airsonic responds with an error"
|
||||||
|
(let [fx (events/good-api-response {} [:_ (:error responses)])]
|
||||||
|
(is (= :error (-> fx :dispatch second))))))
|
||||||
|
|
||||||
(deftest user-notifications
|
(deftest user-notifications
|
||||||
(testing "Should be able to display a message with an assigned level"
|
(testing "Should be able to display a message with an assigned level"
|
||||||
(is (= :error (:level (first-notification (events/show-notification {} [:_ :error "foo"])))))
|
(is (= :error (:level (first-notification (events/show-notification {} [:_ :error "foo"])))))
|
||||||
|
|
|
||||||
14
test/cljs/airsonic_ui/fixtures.cljs
Normal file
14
test/cljs/airsonic_ui/fixtures.cljs
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
(ns airsonic-ui.fixtures)
|
||||||
|
|
||||||
|
(def responses {:error {:subsonic-response
|
||||||
|
{:error {:code 40
|
||||||
|
:message "Wrong username or password"}
|
||||||
|
:status "failed"
|
||||||
|
:version "1.15.0"}}
|
||||||
|
:ok {:subsonic-response
|
||||||
|
{:scanStatus {:count 10326
|
||||||
|
:scanning false}
|
||||||
|
:status "ok"
|
||||||
|
:version "1.15.0"}}
|
||||||
|
:auth-success {:subsonic-response {:status "ok"
|
||||||
|
:version "1.15.0"}}})
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
(ns airsonic-ui.utils.api-test
|
(ns airsonic-ui.utils.api-test
|
||||||
(:require [cljs.test :refer [deftest testing is]]
|
(:require [cljs.test :refer [deftest testing is]]
|
||||||
[clojure.string :as str]
|
[clojure.string :as str]
|
||||||
|
[airsonic-ui.fixtures :refer [responses]]
|
||||||
[airsonic-ui.utils.api :as api]))
|
[airsonic-ui.utils.api :as api]))
|
||||||
|
|
||||||
(defn- url
|
(defn- url
|
||||||
|
|
@ -9,17 +10,7 @@
|
||||||
(api/url server endpoint {}))
|
(api/url server endpoint {}))
|
||||||
|
|
||||||
(def fixtures
|
(def fixtures
|
||||||
{:default-url (url "http://localhost:8080" "ping")
|
{:default-url (url "http://localhost:8080" "ping")})
|
||||||
:responses {:error {:subsonic-response
|
|
||||||
{:error {:code 40
|
|
||||||
:message "Wrong username or password"}
|
|
||||||
:status "failed"
|
|
||||||
:version "1.15.0"}}}
|
|
||||||
:ok {:subsonic-response
|
|
||||||
{:scanStatus {:count 10326
|
|
||||||
:scanning false}
|
|
||||||
:status "ok"
|
|
||||||
:version "1.15.0"}}})
|
|
||||||
|
|
||||||
(deftest general-url-construction
|
(deftest general-url-construction
|
||||||
(testing "Handles missing slashes"
|
(testing "Handles missing slashes"
|
||||||
|
|
@ -43,14 +34,14 @@
|
||||||
|
|
||||||
(deftest response-handling
|
(deftest response-handling
|
||||||
(testing "Should unwrap responses"
|
(testing "Should unwrap responses"
|
||||||
(let [response (get-in fixtures [:responses :ok])]
|
(let [response (:ok responses)]
|
||||||
(is (= (get-in response [:subsonic-response :scanStatus])
|
(is (= (get-in response [:subsonic-response :scanStatus])
|
||||||
(api/unwrap-response response)))))
|
(api/unwrap-response response)))))
|
||||||
(testing "Should detect errors"
|
(testing "Should detect errors"
|
||||||
(is (true? (api/is-error? (get-in fixtures [:responses :error]))))
|
(is (true? (api/is-error? (:error responses))))
|
||||||
(is (false? (api/is-error? (get-in fixtures [:responses :ok])))))
|
(is (false? (api/is-error? (:ok responses)))))
|
||||||
(testing "Should throw an informative error when trying to unwrap an erroneous response"
|
(testing "Should throw an informative error when trying to unwrap an erroneous response"
|
||||||
(let [error-response (get-in fixtures [:responses :error])]
|
(let [error-response (:error responses)]
|
||||||
(is (thrown? ExceptionInfo (api/unwrap-response error-response)))
|
(is (thrown? ExceptionInfo (api/unwrap-response error-response)))
|
||||||
(try
|
(try
|
||||||
(api/unwrap-response error-response)
|
(api/unwrap-response error-response)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue