1// Copyright (C) 2022 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import {exists} from './utils'; 16 17// Errors in JavaScript are (sadly) extremely free form. Three common 18// cases are: 19// - the error is a string 20// - the error object itself has a 'message' field (normally because 21// its an instance of Error or a subclass of Error). 22// - the outer object wraps an error under the 'error' key. As in: 23// https://google.github.io/styleguide/jsoncstyleguide.xml#Reserved_Property_Names_in_the_error_object 24// TODO(hjd): Can this last case actually occur for us? Maybe this 25// too closely followed the code from flash station? 26interface ErrorLikeObject { 27 message?: unknown; 28 error?: {message?: unknown}; 29 stack?: unknown; 30 code?: unknown; 31} 32 33// Attempt to coerce an error object into a string message. 34// Sometimes an error message is wrapped in an Error object, sometimes not. 35export function getErrorMessage(e: unknown | undefined | null) { 36 if (exists(e) && typeof e === 'object') { 37 const errorObject = e as ErrorLikeObject; 38 if (exists(errorObject.message)) { 39 // regular Error Object 40 return String(errorObject.message); 41 } else if (exists(errorObject.error) && exists(errorObject.error.message)) { 42 // API result 43 return String(errorObject.error.message); 44 } 45 } 46 const asString = String(e); 47 if (asString === '[object Object]') { 48 try { 49 return JSON.stringify(e); 50 } catch (stringifyError) { 51 // ignore failures and just fall through 52 } 53 } 54 return asString; 55} 56 57// Occasionally operations using the cache API throw: 58// 'UnknownError: Unexpected internal error. {}' 59// It's not clear under which circumstances this can occur. A dive of 60// the Chromium code didn't shed much light: 61// https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/modules/cache_storage/cache_storage_error.cc;l=26;drc=4cfe86482b000e848009077783ba35f83f3c3cfe 62// https://source.chromium.org/chromium/chromium/src/+/main:content/browser/cache_storage/cache_storage_cache.cc;l=1686;drc=ab68c05beb790d04d1cb7fd8faa0a197fb40d399 63// Given the error is not actionable at present and caching is 'best 64// effort' in any case ignore this error. We will want to throw for 65// errors in general though so as not to hide errors we actually could 66// fix. 67// See b/227785665 for an example. 68export function ignoreCacheUnactionableErrors<T>(e: unknown, result: T): T { 69 if (getErrorMessage(e).includes('UnknownError')) { 70 return result; 71 } else { 72 throw e; 73 } 74} 75