This commit is contained in:
David Lebee 2021-07-29 17:12:21 -04:00
parent 99bcbf45ed
commit 2972b38a1a
3 changed files with 41 additions and 32 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@poweredsoft/ngx-data",
"version": "0.0.19",
"version": "0.0.20",
"peerDependencies": {
"@angular/common": "^8.2.4",
"@angular/core": "^8.2.4",

View File

@ -1,4 +1,4 @@
import { HttpClient, HttpErrorResponse } from "@angular/common/http";
import { HttpClient, HttpErrorResponse, HttpResponse } from "@angular/common/http";
import { DataSource, IDataSource, IDataSourceCommandAdapterOptions, IDataSourceError, IDataSourceErrorMessage, IDataSourceOptions, IDataSourceQueryAdapterOptions, IDataSourceTransportOptions, IDataSourceValidationError, IQueryCriteria, IQueryExecutionGroupResult, IQueryExecutionResult, IResolveCommandModelEvent } from "@poweredsoft/data";
import { Observable, of, throwError } from "rxjs";
import { catchError, switchMap } from "rxjs/operators";
@ -43,8 +43,7 @@ export class HttpDataSourceOptionsBuilder<TModel, TKey> {
return this;
}
public beforeRead<TDynamicQuery extends IQueryCriteria>(beforeRead: (query: TDynamicQuery) => Observable<TDynamicQuery>)
{
public beforeRead<TDynamicQuery extends IQueryCriteria>(beforeRead: (query: TDynamicQuery) => Observable<TDynamicQuery>) {
this._beforeRead = beforeRead;
return this;
}
@ -85,23 +84,9 @@ export class HttpDataSourceOptionsBuilder<TModel, TKey> {
return this;
}
private _handleErrorPipe(err: HttpErrorResponse) : Observable<IDataSourceError> {
if (err.status == 500) {
return throwError(<IDataSourceErrorMessage>{
type: 'message',
message: 'UNEXPECTED_ERROR_MESSAGE'
});
}
if (err.status == 400)
{
if (err.error && err.error.errors)
return throwError(<IDataSourceValidationError>{
type: 'validation',
errors: err.error.errors
});
private _messageErrorHandler(err: HttpErrorResponse) {
if (typeof err.error == "object") {
// if status not okay then its an exception error
if (err.error.hasOwnProperty('Message') && typeof (err.error['Message']) == "string") {
return throwError(<IDataSourceErrorMessage>{
@ -109,6 +94,12 @@ export class HttpDataSourceOptionsBuilder<TModel, TKey> {
message: err.error['Message']
});
}
else if (err.error.hasOwnProperty('message') && typeof (err.error['message']) == "string") {
return throwError(<IDataSourceErrorMessage>{
type: 'message',
message: err.error['message']
});
}
}
// general error message
@ -125,9 +116,22 @@ export class HttpDataSourceOptionsBuilder<TModel, TKey> {
});
}
private _handleErrorPipe(err: HttpErrorResponse): Observable<IDataSourceError> {
if (err.status == 400) {
if (err.error && err.error.errors)
return throwError(<IDataSourceValidationError>{
type: 'validation',
errors: err.error.errors
});
}
return this._messageErrorHandler(err);
}
public addCommandByCallback<TCommand, TCommandResult>(name: string, commandHandler: (command: TCommand) => Observable<TCommandResult>, resolveCommandModel?: (event: IResolveCommandModelEvent<TModel>) => Observable<TCommand & any>) {
const handleWrapper = command => {
return commandHandler(command).pipe(catchError(this._handleErrorPipe));
return commandHandler(command).pipe(catchError(err => this._handleErrorPipe.bind(this)));
};
this._commands[name] = <IDataSourceCommandAdapterOptions<TModel>>{
@ -146,7 +150,7 @@ export class HttpDataSourceOptionsBuilder<TModel, TKey> {
return finalBeforeCommand(command)
.pipe(
switchMap(finalCommand => {
return this.http.post<TCommandResult>(url, finalCommand).pipe(catchError(this._handleErrorPipe));
return this.http.post<TCommandResult>(url, finalCommand).pipe(catchError(this._handleErrorPipe.bind(this)));
})
);
};

View File

@ -75,6 +75,11 @@ export class AppComponent implements OnInit {
this.dataSource.data$.subscribe(newData => {
this.latestData = newData;
});
this.dataSource.notifyMessage$.subscribe(message => {
if (message.type == 'error')
alert(message.message);
});
}
refresh() {