Skip to content

Commit b97f39b

Browse files
committed
better error with Request Entitlement and sending user_id
1 parent 4a5239e commit b97f39b

File tree

4 files changed

+168
-17
lines changed

4 files changed

+168
-17
lines changed

env_ai

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ VITE_OPB_SERVER_SESSION_PASSWORD=asidudhiuh33875
1010
VITE_OBP_REDIS_URL=redis://127.0.0.1:6379
1111

1212
### Opey Configuration ###
13-
VITE_CHATBOT_ENABLED=true
13+
VITE_CHATBOT_ENABLED=false
1414
VITE_CHATBOT_URL=http://localhost:5000
1515

1616
### OAuth2/OIDC Configuration ###

server/controllers/UserController.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { Request, Response } from 'express'
3030
import OBPClientService from '../services/OBPClientService'
3131
import { Service, Container } from 'typedi'
3232
import { OAuth2Service } from '../services/OAuth2Service'
33+
import { DEFAULT_OBP_API_VERSION } from '../../shared-constants'
3334

3435
@Service()
3536
@Controller('/user')
@@ -129,9 +130,25 @@ export class UserController {
129130
}
130131
}
131132

133+
// Get actual user ID from OBP-API
134+
let obpUserId = oauth2User.sub // Default to sub if OBP call fails
135+
try {
136+
const version = process.env.VITE_OBP_API_VERSION ?? DEFAULT_OBP_API_VERSION
137+
const obpUser = await this.obpClientService.get(
138+
`/obp/${version}/users/current`,
139+
session['clientConfig']
140+
)
141+
if (obpUser && obpUser.user_id) {
142+
obpUserId = obpUser.user_id
143+
console.log('UserController: Got OBP user ID:', obpUserId)
144+
}
145+
} catch (error) {
146+
console.warn('UserController: Could not fetch OBP user ID, using token sub:', error)
147+
}
148+
132149
// Return user info in format compatible with frontend
133150
return response.json({
134-
user_id: oauth2User.sub,
151+
user_id: obpUserId,
135152
username: oauth2User.username,
136153
email: oauth2User.email,
137154
email_verified: oauth2User.email_verified,

server/services/OBPClientService.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@
2828
import { Service } from 'typedi'
2929
import { DEFAULT_OBP_API_VERSION } from '../../shared-constants'
3030

31+
// Custom error class to preserve HTTP status codes
32+
class OBPAPIError extends Error {
33+
status: number
34+
constructor(status: number, message: string) {
35+
super(message)
36+
this.status = status
37+
this.name = 'OBPAPIError'
38+
}
39+
}
40+
3141
// OAuth2 Bearer token configuration
3242
interface OAuth2Config {
3343
accessToken: string
@@ -140,7 +150,7 @@ export default class OBPClientService {
140150
if (!response.ok) {
141151
const errorText = await response.text()
142152
console.error('OBPClientService: GET request failed:', response.status, errorText)
143-
throw new Error(`HTTP ${response.status}: ${errorText}`)
153+
throw new OBPAPIError(response.status, errorText)
144154
}
145155

146156
return await response.json()
@@ -170,7 +180,7 @@ export default class OBPClientService {
170180
if (!response.ok) {
171181
const errorText = await response.text()
172182
console.error('OBPClientService: POST request failed:', response.status, errorText)
173-
throw new Error(`HTTP ${response.status}: ${errorText}`)
183+
throw new OBPAPIError(response.status, errorText)
174184
}
175185

176186
return await response.json()
@@ -200,7 +210,7 @@ export default class OBPClientService {
200210
if (!response.ok) {
201211
const errorText = await response.text()
202212
console.error('OBPClientService: PUT request failed:', response.status, errorText)
203-
throw new Error(`HTTP ${response.status}: ${errorText}`)
213+
throw new OBPAPIError(response.status, errorText)
204214
}
205215

206216
return await response.json()
@@ -228,7 +238,7 @@ export default class OBPClientService {
228238
if (!response.ok) {
229239
const errorText = await response.text()
230240
console.error('OBPClientService: DELETE request failed:', response.status, errorText)
231-
throw new Error(`HTTP ${response.status}: ${errorText}`)
241+
throw new OBPAPIError(response.status, errorText)
232242
}
233243

234244
return await response.json()

src/components/Preview.vue

Lines changed: 135 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,32 +184,156 @@ const highlightCode = (json) => {
184184
}
185185
}
186186
const submitEntitlement = async () => {
187-
requiredRoles.value.forEach(async (formRole, idx) => {
187+
for (const [idx, formRole] of requiredRoles.value.entries()) {
188+
const role = roleForm[`role${formRole.role}${idx}`]
189+
188190
if (formRole.requires_bank_id) {
189-
const role = roleForm[`role${formRole.role}${idx}`]
191+
// Bank-level entitlement
190192
const bankId = roleForm[`bankId${formRole.role}${idx}`]
191-
if (role && bankId && isUserLogon) {
193+
194+
if (!role || !bankId) {
195+
ElNotification({
196+
duration: elMessageDuration,
197+
title: 'Missing Information',
198+
message: 'Bank ID is required for this role.',
199+
position: 'bottom-right',
200+
type: 'error'
201+
})
202+
continue
203+
}
204+
205+
if (!isUserLogon) {
206+
ElNotification({
207+
duration: elMessageDuration,
208+
title: 'Not Authenticated',
209+
message: 'Please login to request this role.',
210+
position: 'bottom-right',
211+
type: 'error'
212+
})
213+
continue
214+
}
215+
216+
try {
192217
const response = await createEntitlement(bankId, role)
193-
let type = 'success'
194-
if ('code' in response && response['code'] >= 400) {
195-
type = 'error'
218+
219+
// Check if response is an error object (from superagent)
220+
const isError = response && response.error && response.error.response
221+
const errorBody = isError ? response.error.response.body : null
222+
const statusCode = isError ? response.error.status : null
223+
224+
if (isError && errorBody && errorBody.code >= 400) {
225+
// Parse error message from body
226+
let errorMessage = 'Failed to create entitlement'
227+
if (errorBody.message) {
228+
// Message might be double-encoded JSON string
229+
try {
230+
const parsedMessage = JSON.parse(errorBody.message)
231+
errorMessage = parsedMessage.message || errorBody.message
232+
} catch {
233+
errorMessage = errorBody.message
234+
}
235+
}
236+
237+
ElNotification({
238+
duration: elMessageDuration,
239+
title: `Error (${errorBody.code})`,
240+
message: errorMessage,
241+
position: 'bottom-right',
242+
type: 'error'
243+
})
244+
} else {
245+
// Success
246+
ElNotification({
247+
duration: elMessageDuration,
248+
title: 'Success',
249+
message: `Entitlement "${role}" requested successfully for bank "${bankId}"`,
250+
position: 'bottom-right',
251+
type: 'success'
252+
})
196253
}
254+
} catch (error: any) {
255+
ElNotification({
256+
duration: elMessageDuration,
257+
title: 'Request Failed',
258+
message: error.message || 'An error occurred while requesting the entitlement',
259+
position: 'bottom-right',
260+
type: 'error'
261+
})
262+
}
263+
} else {
264+
// System-wide entitlement (no bank_id required)
265+
if (!role) {
266+
ElNotification({
267+
duration: elMessageDuration,
268+
title: 'Missing Information',
269+
message: 'Role name is required.',
270+
position: 'bottom-right',
271+
type: 'error'
272+
})
273+
continue
274+
}
275+
276+
if (!isUserLogon) {
197277
ElNotification({
198278
duration: elMessageDuration,
199-
message: response.message,
279+
title: 'Not Authenticated',
280+
message: 'Please login to request this role.',
200281
position: 'bottom-right',
201-
type
282+
type: 'error'
202283
})
203-
} else {
284+
continue
285+
}
286+
287+
try {
288+
// System-wide entitlement uses empty string for bank_id
289+
const response = await createEntitlement('', role)
290+
291+
// Check if response is an error object (from superagent)
292+
const isError = response && response.error && response.error.response
293+
const errorBody = isError ? response.error.response.body : null
294+
const statusCode = isError ? response.error.status : null
295+
296+
if (isError && errorBody && errorBody.code >= 400) {
297+
// Parse error message from body
298+
let errorMessage = 'Failed to create entitlement'
299+
if (errorBody.message) {
300+
// Message might be double-encoded JSON string
301+
try {
302+
const parsedMessage = JSON.parse(errorBody.message)
303+
errorMessage = parsedMessage.message || errorBody.message
304+
} catch {
305+
errorMessage = errorBody.message
306+
}
307+
}
308+
309+
ElNotification({
310+
duration: elMessageDuration,
311+
title: `Error (${errorBody.code})`,
312+
message: errorMessage,
313+
position: 'bottom-right',
314+
type: 'error'
315+
})
316+
} else {
317+
// Success
318+
ElNotification({
319+
duration: elMessageDuration,
320+
title: 'Success',
321+
message: `System-wide entitlement "${role}" requested successfully`,
322+
position: 'bottom-right',
323+
type: 'success'
324+
})
325+
}
326+
} catch (error: any) {
204327
ElNotification({
205328
duration: elMessageDuration,
206-
message: 'Bank Id is required.',
329+
title: 'Request Failed',
330+
message: error.message || 'An error occurred while requesting the entitlement',
207331
position: 'bottom-right',
208332
type: 'error'
209333
})
210334
}
211335
}
212-
})
336+
}
213337
}
214338
onBeforeMount(async () => {
215339
const route = useRoute()

0 commit comments

Comments
 (0)