@@ -4,9 +4,7 @@ import { API } from "stoat.js";
44
55const RE_BREAK = / \s * \n \s * / g;
66
7- function cleanError (
8- error : unknown ,
9- ) : { type ?: never } | { message ?: never } | string | undefined {
7+ function cleanError ( error : unknown ) {
108 // Attempt to parse the incoming error as JSON if it is a string,
119 // as some errors (e.g on login) are sent to this function as a string,
1210 // which then causes the error message to be unlocalised and unhelpful.
@@ -15,11 +13,9 @@ function cleanError(
1513 return JSON . parse ( error ) ;
1614 } catch {
1715 // Ignore JSON parse errors
18- return error ;
1916 }
2017 }
21-
22- return error as { type ?: never } | { message ?: never } | undefined ;
18+ return error ;
2319}
2420
2521/**
@@ -30,15 +26,13 @@ export function useError() {
3026
3127 return ( error : unknown ) => {
3228 error = cleanError ( error ) ;
29+ console . error ( error ) ;
3330
3431 // TODO: HTTP errors
3532
3633 // handle Revolt API errors
37- if (
38- ( error as { type ?: never } | undefined ) ?. type &&
39- typeof ( error as { type : never } ) . type === "string"
40- ) {
41- const err = error as API . Error ;
34+ if ( typeof ( error as { type : never } ) ?. type === "string" ) {
35+ const err = error as API . Error | { type : "SocketError" } ;
4236
4337 switch ( err . type ) {
4438 case "AlreadyFriends" :
@@ -96,7 +90,7 @@ export function useError() {
9690 case "InvalidCredentials" :
9791 return t `Provided email or password is wrong.` ;
9892 case "InvalidSession" :
99- return t `Please log in again. ` ;
93+ return t `You were logged out! ` ;
10094 case "InvalidUsername" :
10195 return t `This username is not allowed.` ;
10296 case "MissingPermission" :
@@ -132,6 +126,8 @@ export function useError() {
132126 return t `This account is not activated! Please check your account's inbox and try again.` ;
133127 case "TotpAlreadyEnabled" :
134128 return t `Multi-factor authentication is already enabled for this account.` ;
129+ case "SocketError" :
130+ return t `The web socket was unable to connect to the backend.` ;
135131
136132 // unreachable errors (in theory)
137133 case "FileTooLarge" :
@@ -169,10 +165,7 @@ export function useError() {
169165 }
170166
171167 // pass-through pre-localised errors with new Error({ message: <> })
172- if (
173- ( error as { message ?: never } | undefined ) ?. message &&
174- typeof ( error as { message : never } ) . message === "string"
175- ) {
168+ if ( typeof ( error as { message : never } ) ?. message === "string" ) {
176169 const message = ( error as { message : string } ) . message . trim ( ) ;
177170 if ( message ) return message ;
178171 } else if ( typeof error === "string" ) {
0 commit comments