@@ -65,6 +65,16 @@ const {
6565 deleteTypeIndexRegistration
6666} = solidLogicSingleton . typeIndex
6767
68+ function formatDynamicError ( error : unknown , contextLabel : string ) : string {
69+ const e = error as any
70+ const name = e ?. name || 'Error'
71+ const status = typeof e ?. status === 'number' ? ` (HTTP ${ e . status } )` : ''
72+ const message = e ?. message
73+ ? String ( e . message )
74+ : ( typeof error === 'string' ? error : 'No additional details provided' )
75+ return `${ contextLabel } : ${ name } ${ status } - ${ message } `
76+ }
77+
6878/**
6979 * Resolves with the logged in user's WebID
7080 *
@@ -125,6 +135,10 @@ export async function ensureLoadedPreferences (
125135 } */
126136 try {
127137 context = await ensureLoadedProfile ( context )
138+ if ( ! context . me ) {
139+ context . preferencesFileError = 'Not logged in, so preferences were not loaded.'
140+ return context
141+ }
128142
129143 // console.log('back in Solid UI after logInLoadProfile', context)
130144 const preferencesFile = await loadPreferences ( context . me as NamedNode )
@@ -137,30 +151,47 @@ export async function ensureLoadedPreferences (
137151 if ( err instanceof UnauthorizedError ) {
138152 m2 =
139153 'Oops — you are not authenticated (properly logged in), so SolidOS cannot read your preferences file. Try logging out and then logging back in.'
154+ context . preferencesFileError = m2
140155 alert ( m2 )
156+ return context
141157 } else if ( err instanceof CrossOriginForbiddenError ) {
142- m2 = `Unauthorized: Assuming preference file blocked for origin ${ window . location . origin } `
158+ m2 = `Unauthorized: preference file request was blocked for origin ${ window . location . origin } . `
143159 context . preferencesFileError = m2
144160 return context
145161 } else if ( err instanceof SameOriginForbiddenError ) {
146162 m2 =
147- 'You are not authorized to read your preference file. This may be because you are using an untrusted web app.'
163+ 'You are not authorized to read your preference file from this app context.'
164+ context . preferencesFileError = m2
148165 debug . warn ( m2 )
149166 return context
150167 } else if ( err instanceof NotEditableError ) {
151168 m2 =
152- 'You are not authorized to edit your preference file. This may be because you are using an untrusted web app.'
169+ 'You are not authorized to edit your preference file from this app context.'
170+ context . preferencesFileError = m2
153171 debug . warn ( m2 )
154172 return context
155173 } else if ( err instanceof WebOperationError ) {
156- m2 =
157- 'You are not authorized to edit your preference file. This may be because you are using an untrusted web app.'
174+ m2 = formatDynamicError ( err , 'Preference file web operation failed' )
175+ context . preferencesFileError = m2
158176 debug . warn ( m2 )
177+ return context
159178 } else if ( err instanceof FetchError ) {
160- m2 = `Strange: Error ${ err . status } trying to read your preference file.${ err . message } `
179+ if ( err . status === 404 ) {
180+ m2 = 'Your preferences file was not found (404). It may not exist yet.'
181+ context . preferencesFileError = m2
182+ debug . warn ( m2 )
183+ return context
184+ }
185+ m2 = formatDynamicError ( err , 'Error reading your preferences file' )
186+ context . preferencesFileError = m2
187+ debug . warn ( m2 )
161188 alert ( m2 )
189+ return context
162190 } else {
163- throw new Error ( `(via loadPrefs) ${ err } ` )
191+ m2 = formatDynamicError ( err , 'Unexpected error while loading preferences' )
192+ context . preferencesFileError = m2
193+ debug . error ( m2 )
194+ return context
164195 }
165196 }
166197 return context
@@ -183,14 +214,17 @@ export async function ensureLoadedProfile (
183214 try {
184215 const logInContext = await ensureLoggedIn ( context )
185216 if ( ! logInContext . me ) {
186- throw new Error ( 'Could not log in' )
217+ const message = 'Could not log in; skipping profile load.'
218+ debug . log ( message )
219+ return context
187220 }
188221 context . publicProfile = await loadProfile ( logInContext . me )
189222 } catch ( err ) {
223+ const message = formatDynamicError ( err , 'Unable to load your profile' )
190224 if ( context . div && context . dom ) {
191- context . div . appendChild ( widgets . errorMessageBlock ( context . dom , err . message ) )
225+ context . div . appendChild ( widgets . errorMessageBlock ( context . dom , message ) )
192226 }
193- throw new Error ( `Can't log in: ${ err } ` )
227+ throw new Error ( message )
194228 }
195229 return context
196230}
@@ -1049,17 +1083,21 @@ export function newAppInstance (
10491083export async function getUserRoles ( ) : Promise < Array < NamedNode > > {
10501084 try {
10511085 const { me, preferencesFile, preferencesFileError } = await ensureLoadedPreferences ( { } )
1052- if ( ! preferencesFile || preferencesFileError ) {
1086+ if ( preferencesFileError ) {
10531087 throw new Error ( preferencesFileError )
10541088 }
1089+ if ( ! preferencesFile ) {
1090+ const authState = me ? `logged in as ${ me . value || me . uri } ` : 'not logged in'
1091+ throw new Error ( `Preferences file unavailable (${ authState } )` )
1092+ }
10551093 return solidLogicSingleton . store . each (
10561094 me ,
10571095 ns . rdf ( 'type' ) ,
10581096 null ,
10591097 preferencesFile . doc ( )
10601098 ) as NamedNode [ ]
10611099 } catch ( error ) {
1062- debug . warn ( 'Unable to fetch your preferences - this was the error: ' , error )
1100+ debug . warn ( formatDynamicError ( error , 'Unable to fetch your preferences' ) )
10631101 }
10641102 return [ ]
10651103}
0 commit comments