I wrote a function in Typescript which takes required parameters, optional parameters and parameters with default value. I want to use this function for error handling of http requests. I am not sure, if my solution is good, so I wanted to know the opinion of the stackoverflowCode Review community.
Here is my function:
public handleError<T>(
operation: string = 'operation',
customErrorMessage: string,
valueToReturn: T,
logError = true,
showSnackbar?: boolean,
showHttpErrorResponse?: boolean
): (error: any) => Observable<any> {
return (error: any): Observable<T | Error> => {
if (logError) {
console.error(error.message);
}
if (showSnackbar) {
if (showHttpErrorResponse) {
this.snackbar.open(error.message, 'OK');
} else {
this.snackbar.open(customErrorMessage, 'OK');
}
}
return showHttpErrorResponse ? of(new Error(error.message)) : of(valueToReturn as T);
};
}
Here is an example of calling:
this.httpClient.get('/', { observe: 'response', responseType: 'text' }).pipe(
catchError(
this.errorService.handleError('getSysName', '', null, true, false, true)
)
);
My Questions:
- How is the selections of the parameter types (usage of required, optional and default parameters)? If it is not good, how would you improve it?
- How is the naming of the parameters?
- How is the order of the parameters? If it is not good, how would you order the parameters?
- Do you have also improvement suggestions for the body of the function?
- Do you have completely different suggestions for improvement?