Skip to main content
deleted 3 characters in body; edited tags; edited title
Source Link
200_success
  • 145.7k
  • 22
  • 192
  • 481

When to use required parameter, optional parameter and default value parameter Typescript error handler

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:

  1. 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?
  2. How is the naming of the parameters?
  3. How is the order of the parameters? If it is not good, how would you order the parameters?
  4. Do you have also improvement suggestions for the body of the function?
  5. Do you have completely different suggestions for improvement?

When to use required parameter, optional parameter and default value parameter

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 stackoverflow 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:

  1. 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?
  2. How is the naming of the parameters?
  3. How is the order of the parameters? If it is not good, how would you order the parameters?
  4. Do you have also improvement suggestions for the body of the function?
  5. Do you have completely different suggestions for improvement?

Typescript error handler

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 Code 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:

  1. 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?
  2. How is the naming of the parameters?
  3. How is the order of the parameters? If it is not good, how would you order the parameters?
  4. Do you have also improvement suggestions for the body of the function?
  5. Do you have completely different suggestions for improvement?
deleted 1 character in body
Source Link

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 stackoverflow 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) {
        // code for logging comes hereconsole.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:

  1. 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?
  2. How is the naming of the parameters?
  3. How is the order of the parameters? If it is not good, how would you order the parameters?
  4. Do you have also improvement suggestions for the body of the function?
  5. Do you have completely different suggestions for improvement?

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 stackoverflow 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) {
        // code for logging comes here
      }

      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:

  1. 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?
  2. How is the naming of the parameters?
  3. How is the order of the parameters? If it is not good, how would you order the parameters?
  4. Do you have also improvement suggestions for the body of the function?
  5. Do you have completely different suggestions for improvement?

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 stackoverflow 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:

  1. 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?
  2. How is the naming of the parameters?
  3. How is the order of the parameters? If it is not good, how would you order the parameters?
  4. Do you have also improvement suggestions for the body of the function?
  5. Do you have completely different suggestions for improvement?
Source Link

When to use required parameter, optional parameter and default value parameter

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 stackoverflow 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) {
        // code for logging comes here
      }

      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:

  1. 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?
  2. How is the naming of the parameters?
  3. How is the order of the parameters? If it is not good, how would you order the parameters?
  4. Do you have also improvement suggestions for the body of the function?
  5. Do you have completely different suggestions for improvement?