/** * Copyright (c) 2013-present, Facebook, Inc. * * @emails react-core * @flow */ import React from 'react'; import type {Node} from 'react'; function replaceArgs(msg: string, argList: Array): string { let argIdx = 0; return msg.replace(/%s/g, function() { const arg = argList[argIdx++]; return arg === undefined ? '[missing argument]' : arg; }); } // When the message contains a URL (like https://fb.me/react-refs-must-have-owner), // make it a clickable link. function urlify(str: string): Node { const urlRegex = /(https:\/\/fb\.me\/[a-z\-]+)/g; const segments = str.split(urlRegex); return segments.map((message, i) => { if (i % 2 === 1) { return ( {message} ); } return message; }); } // `?invariant=123&args[]=foo&args[]=bar` // or `// ?invariant=123&args[0]=foo&args[1]=bar` function parseQueryString( search: string, ): ?{|code: string, args: Array|} { const rawQueryString = search.substring(1); if (!rawQueryString) { return null; } let code = ''; let args = []; const queries = rawQueryString.split('&'); for (let i = 0; i < queries.length; i++) { const query = decodeURIComponent(queries[i]); if (query.indexOf('invariant=') === 0) { code = query.slice(10); } else if (query.indexOf('args[') === 0) { args.push(query.slice(query.indexOf(']=') + 2)); } } return {args, code}; } function ErrorResult(props: {|code: ?string, msg: string|}) { const code = props.code; const errorMsg = props.msg; if (!code) { return (

When you encounter an error, you'll receive a link to this page for that specific error and we'll show you the full error text.

); } return (

The full text of the error you just encountered is:

{urlify(errorMsg)}
); } function ErrorDecoder(props: {| errorCodesString: string, location: {search: string}, |}) { let code = null; let msg = ''; const errorCodes = JSON.parse(props.errorCodesString); const parseResult = parseQueryString(props.location.search || ''); if (parseResult != null) { code = parseResult.code; msg = replaceArgs(errorCodes[code], parseResult.args); } return ; } export default ErrorDecoder;