Skip to content

Commit

Permalink
Display actual synapse errors
Browse files Browse the repository at this point in the history
  • Loading branch information
beastafk committed Sep 4, 2024
1 parent 8eaaaa5 commit b3f9d20
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions src/synapse/authProvider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { AuthProvider, Options, fetchUtils } from "react-admin";
import { AuthProvider, HttpError, Options, fetchUtils } from "react-admin";

import storage from "../storage";

type SynapseError = {
errcode: string;
error: string;
}

const displayError = (errcode: string, status: number, message: string) => `${errcode} (${status}): ${message}`;

const authProvider: AuthProvider = {
// called when the user attempts to log in
login: async ({
Expand Down Expand Up @@ -50,7 +57,23 @@ const authProvider: AuthProvider = {
const decoded_base_url = window.decodeURIComponent(base_url);
const login_api_url = decoded_base_url + "/_matrix/client/r0/login";

const { json } = await fetchUtils.fetchJson(login_api_url, options);
let response;
try {
response = await fetchUtils.fetchJson(login_api_url, options);
} catch(err) {
const error = err as HttpError;
const errorStatus = error.status;
const errorBody = error.body as SynapseError;

return Promise.reject(
new HttpError(
displayError(errorBody.errcode, errorStatus, errorBody.error),
errorStatus,
)
);
}

const json = response.json;
storage.setItem("home_server", json.home_server);
storage.setItem("user_id", json.user_id);
storage.setItem("access_token", json.access_token);
Expand All @@ -77,10 +100,11 @@ const authProvider: AuthProvider = {
}
},
// called when the API returns an error
checkError: ({ status }: { status: number }) => {
console.log("checkError " + status);
checkError: (err: HttpError) => {
const errorBody = err.body as SynapseError;
const status = err.status;
if (status === 401 || status === 403) {
return Promise.reject();
return Promise.reject({message: displayError(errorBody.errcode, status, errorBody.error)});
}
return Promise.resolve();
},
Expand Down

0 comments on commit b3f9d20

Please sign in to comment.