mirror of
				https://github.com/CorentinTh/it-tools.git
				synced 2025-11-03 21:43:21 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			25 lines
		
	
	
		
			437 B
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			25 lines
		
	
	
		
			437 B
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
import _ from 'lodash';
 | 
						|
 | 
						|
export { getErrorMessageIfThrows };
 | 
						|
 | 
						|
function getErrorMessageIfThrows(cb: () => unknown) {
 | 
						|
  try {
 | 
						|
    cb();
 | 
						|
    return undefined;
 | 
						|
  } catch (err) {
 | 
						|
    if (_.isString(err)) {
 | 
						|
      return err;
 | 
						|
    }
 | 
						|
 | 
						|
    if (_.isError(err)) {
 | 
						|
      return err.message;
 | 
						|
    }
 | 
						|
 | 
						|
    if (_.isObject(err) && _.has(err, 'message')) {
 | 
						|
      return (err as { message: string }).message;
 | 
						|
    }
 | 
						|
 | 
						|
    return 'An error as occurred.';
 | 
						|
  }
 | 
						|
}
 |