Skip to content

Commit 1775006

Browse files
committed
error service
1 parent 9d7603d commit 1775006

File tree

4 files changed

+37
-34
lines changed

4 files changed

+37
-34
lines changed

src/@seed/api/cycle/cycle.service.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import type { HttpErrorResponse } from '@angular/common/http'
22
import { HttpClient } from '@angular/common/http'
33
import { inject, Injectable } from '@angular/core'
44
import type { Observable } from 'rxjs'
5-
import { BehaviorSubject, catchError, map, tap, throwError } from 'rxjs'
5+
import { BehaviorSubject, catchError, map, tap } from 'rxjs'
66
import { OrganizationService } from '@seed/api/organization'
7+
import { ErrorService } from '@seed/services/error/error.service'
78
import { SnackbarService } from 'app/core/snackbar/snackbar.service'
89
import type { Cycle, CycleResponse, CyclesResponse } from './cycle.types'
910

@@ -12,6 +13,7 @@ export class CycleService {
1213
private _httpClient = inject(HttpClient)
1314
private _organizationService = inject(OrganizationService)
1415
private _snackBar = inject(SnackbarService)
16+
private _errorService = inject(ErrorService)
1517
private _cycles = new BehaviorSubject<Cycle[]>([])
1618
orgId: number
1719

@@ -31,8 +33,7 @@ export class CycleService {
3133
this._cycles.next(cycles)
3234
}),
3335
catchError((error: HttpErrorResponse) => {
34-
this._snackBar.alert('Error fetching cycles', 'OK', true)
35-
return throwError(() => new Error(error?.message || 'Error fetching cycles'))
36+
return this._errorService.handleError(error, 'Error fetching cycles')
3637
}),
3738
)
3839
.subscribe()
@@ -46,8 +47,7 @@ export class CycleService {
4647
this._snackBar.success(`Created Cycle ${response.cycles.name}`)
4748
}),
4849
catchError((error: HttpErrorResponse) => {
49-
this._snackBar.alert('Error creating cycle', 'OK', true)
50-
return throwError(() => new Error(error?.message || 'Error creating cycle'))
50+
return this._errorService.handleError(error, 'Error creating cycle')
5151
}),
5252
)
5353
}
@@ -59,8 +59,7 @@ export class CycleService {
5959
this._snackBar.success(`Updated Cycle ${response.cycles.name}`, 'OK', true)
6060
}),
6161
catchError((error: HttpErrorResponse) => {
62-
this._snackBar.alert('Error updating cycle', 'OK', true)
63-
return throwError(() => new Error(error?.message || 'Error updating cycle'))
62+
return this._errorService.handleError(error, 'Error updating cycle')
6463
}),
6564
)
6665
}
@@ -72,8 +71,7 @@ export class CycleService {
7271
this._snackBar.success('Cycle deleted', 'OK', true)
7372
}),
7473
catchError((error: HttpErrorResponse) => {
75-
this._snackBar.alert('Cycle deleted successfully', 'OK', true)
76-
return throwError(() => new Error(error?.message || 'Error deleting cycle'))
74+
return this._errorService.handleError(error, 'Error deleting cycle')
7775
}),
7876
)
7977
}

src/@seed/api/organization/organization.service.ts

+8-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import type { HttpErrorResponse } from '@angular/common/http'
22
import { HttpClient } from '@angular/common/http'
33
import { inject, Injectable } from '@angular/core'
44
import type { Observable } from 'rxjs'
5-
import { catchError, map, of, ReplaySubject, Subject, takeUntil, tap, throwError } from 'rxjs'
5+
import { catchError, map, of, ReplaySubject, Subject, takeUntil, tap } from 'rxjs'
6+
import { ErrorService } from '@seed/services/error/error.service'
67
import { SnackbarService } from 'app/core/snackbar/snackbar.service'
78
import { naturalSort } from '../../utils'
89
import { UserService } from '../user'
@@ -29,6 +30,7 @@ export class OrganizationService {
2930
private _organizationUsers = new ReplaySubject<OrganizationUser[]>(1)
3031
private _accessLevelTree = new ReplaySubject<AccessLevelTree>(1)
3132
private _accessLevelInstancesByDepth: AccessLevelsByDepth = {}
33+
private _errorService = inject(ErrorService)
3234
private readonly _unsubscribeAll$ = new Subject<void>()
3335
private _snackBar = inject(SnackbarService)
3436

@@ -65,8 +67,7 @@ export class OrganizationService {
6567
}),
6668
catchError((error: HttpErrorResponse) => {
6769
// TODO need to figure out error handling
68-
console.error('Error occurred fetching organization: ', error.error)
69-
return of({} as Organization)
70+
return this._errorService.handleError(error, 'Error fetching organization')
7071
}),
7172
)
7273
}
@@ -78,8 +79,7 @@ export class OrganizationService {
7879
map((response) => response.users.sort((a, b) => naturalSort(a.last_name, b.last_name))),
7980
tap((users) => { this._organizationUsers.next(users) }),
8081
catchError((error: HttpErrorResponse) => {
81-
console.error('Error fetching organization users:', error)
82-
return of([])
82+
return this._errorService.handleError(error, 'Error fetching organization users')
8383
}),
8484
).subscribe()
8585
}
@@ -100,8 +100,7 @@ export class OrganizationService {
100100
this._accessLevelTree.next(accessLevelTree)
101101
}),
102102
catchError((error: HttpErrorResponse) => {
103-
console.error('Error fetching organization access level tree:', error)
104-
return of({})
103+
return this._errorService.handleError(error, 'Error fetching organization access level tree')
105104
}),
106105
)
107106
.subscribe()
@@ -114,8 +113,7 @@ export class OrganizationService {
114113
this._snackBar.success('Member removed from organization', 'OK', true, 3000)
115114
}),
116115
catchError((error: HttpErrorResponse) => {
117-
console.error('Error deleting organization user:', error)
118-
return throwError(() => new Error(error?.message || 'Error deleting organization user'))
116+
return this._errorService.handleError(error, 'Error removing member from organization')
119117
}),
120118
)
121119
}
@@ -141,9 +139,7 @@ export class OrganizationService {
141139
})
142140
}),
143141
catchError((error: HttpErrorResponse) => {
144-
console.error('Error occurred fetching organization: ', error.error)
145-
this._snackBar.alert(`An error occurred updating the organization: ${error.error}`)
146-
return of(null)
142+
return this._errorService.handleError(error, 'Error updating organization settings')
147143
}),
148144
)
149145
}

src/@seed/api/user/user.service.ts

+7-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { HttpErrorResponse } from '@angular/common/http'
22
import { HttpClient } from '@angular/common/http'
33
import { inject, Injectable } from '@angular/core'
44
import type { Observable } from 'rxjs'
5-
import { catchError, distinctUntilChanged, ReplaySubject, switchMap, take, tap, throwError } from 'rxjs'
5+
import { catchError, distinctUntilChanged, ReplaySubject, switchMap, take, tap } from 'rxjs'
66
import type {
77
CurrentUser,
88
GenerateApiKeyResponse,
@@ -11,14 +11,14 @@ import type {
1111
SetDefaultOrganizationResponse,
1212
UserUpdateRequest,
1313
} from '@seed/api/user'
14-
import { SnackbarService } from 'app/core/snackbar/snackbar.service'
14+
import { ErrorService } from '@seed/services/error/error.service'
1515

1616
@Injectable({ providedIn: 'root' })
1717
export class UserService {
1818
private _httpClient = inject(HttpClient)
1919
private _currentOrganizationId = new ReplaySubject<number>(1)
2020
private _currentUser = new ReplaySubject<CurrentUser>(1)
21-
private _snackBar = inject(SnackbarService)
21+
private _errorService = inject(ErrorService)
2222
currentOrganizationId$ = this._currentOrganizationId.asObservable().pipe(distinctUntilChanged())
2323
currentUser$ = this._currentUser.asObservable()
2424

@@ -63,9 +63,7 @@ export class UserService {
6363
this._currentUser.next(user)
6464
}),
6565
catchError((error: HttpErrorResponse) => {
66-
console.error('Error occurred while updating user:', error.error)
67-
this._snackBar.alert(error?.message || 'Error updating user', 'OK', true)
68-
return throwError(() => new Error(error?.message || 'Error updating user'))
66+
return this._errorService.handleError(error, 'Error updating user')
6967
}),
7068
)
7169
}
@@ -78,9 +76,7 @@ export class UserService {
7876
return this._httpClient.put<{ status: string }>(url, { role })
7977
.pipe(
8078
catchError((error: HttpErrorResponse) => {
81-
console.error('Error occurred while updating user:', error.error)
82-
this._snackBar.alert(error?.message || 'Error updating user', 'OK', true)
83-
return throwError(() => new Error(error?.message || 'Error fetching cycles'))
79+
return this._errorService.handleError(error, 'Error updating user role')
8480
}),
8581
)
8682
}
@@ -91,9 +87,7 @@ export class UserService {
9187
return this._httpClient.put<{ status: string }>(url, { access_level_instance_id: accessLevelInstanceId })
9288
.pipe(
9389
catchError((error: HttpErrorResponse) => {
94-
console.error('Error occurred while updating user:', error.error)
95-
this._snackBar.alert(error?.message || 'Error updating user', 'OK', true)
96-
return throwError(() => new Error(error?.message || 'Error fetching cycles'))
90+
return this._errorService.handleError(error, 'Error updating user access level instance')
9791
}),
9892
)
9993
}
@@ -111,7 +105,7 @@ export class UserService {
111105
this.getCurrentUser().subscribe()
112106
}),
113107
catchError((error: HttpErrorResponse) => {
114-
return throwError(() => error)
108+
return this._errorService.handleError(error, 'Error updating password')
115109
}),
116110
)
117111
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { HttpErrorResponse } from '@angular/common/http'
2+
import { inject, Injectable } from '@angular/core'
3+
import { throwError } from 'rxjs'
4+
import { SnackbarService } from 'app/core/snackbar/snackbar.service'
5+
6+
@Injectable({ providedIn: 'root' })
7+
export class ErrorService {
8+
private _snackBar = inject(SnackbarService)
9+
10+
handleError(error: HttpErrorResponse, defaultMessage: string) {
11+
const errorMessage = (error.error as { message: string })?.message || defaultMessage
12+
this._snackBar.alert(errorMessage, 'OK', true)
13+
return throwError(() => new Error(error?.message || defaultMessage))
14+
}
15+
}

0 commit comments

Comments
 (0)