yannickglt / alfred-devdocs

Alfred workflow for devdocs.io
GNU General Public License v2.0
647 stars 26 forks source link

JSON error: JSON text did not start with array or object and option to allow fragments not set. in JSON #16

Closed chrisyip closed 7 years ago

chrisyip commented 7 years ago

On macOS 10.12.3 (16D32) and Alfred v3.3.1 [806], I got this error:

[ERROR: input.scriptfilter] JSON error: JSON text did not start with array or object and option to allow fragments not set. in JSON:
Warning: Invalid argument supplied for foreach() in Alfred.alfredpreferences/workflows/user.workflow.233DD8DF-9F40-4714-8B5E-8376775DB291/scripts/devdocs.php on line 56

I tried to print the result of file_get_contents('http://devdocs.io/docs/docs.json'), looks fine:

[{"name":"Angular","slug":"angular~2_typescript","type":"angular","links":{"home":"https://angular.io/","code":"https://github.com/angular/angular"},"version":"2 TypeScript","release":"2.4.7","mtime":1487432703,"db_size":3380979}, ...]
yannickglt commented 7 years ago

Hey! Thanks for using Alfred Devdocs. I cannot reproduce the error locally. Do you have the last version of Alfred Devdocs installed? You can install it here.

chrisyip commented 7 years ago

Still have the issue.

I found something might be useful:

In DevDocs's constructor, !isset($doc) || empty($doc) is always true and $this->processDocumentation($key, $query) is never called because of none of keys in $pList['connections'] is exists in $documentations:

foreach ($pList['connections'] as $key => $value) {
  print_r($key . " " . array_key_exists($key, $documentations) . "\n");
  if (array_key_exists($key, $documentations)) {
    $this->checkCache($key);
    $this->processDocumentation($key, $query);
  }
}
$pList['connections'] count: 9
addalltask 0
addalltaskkeyword 0
addtask 0
doc 0
dotaskbasedoncommand 0
nuketask 0
nuketaskkeyword 0
refreshtask 0
removetask 0

Therefore echo $this->workflows->toxml() returns a string that only contains a whitespace.

gamelxy commented 7 years ago

I got the same error, but I found the cache file is not cached completely.

After changed getDocumentations function as blew, it works fine.

 private function getDocumentations() {
    $docFile = self::$cacheDirectory . 'docs.json';
    // Keep the docs in cache during 7 days
    if (!file_exists($docFile) || (filemtime($docFile) <= time() - 86400 * 7)) {
      $docContent = file_get_contents('http://devdocs.io/docs/docs.json');
      file_put_contents($docFile, $docContent);
    } else {
      $docContent = file_get_contents($docFile); 
    }
    $docs = json_decode($docContent);
    $documentations = [];
    foreach ($docs as $doc) {
      $doc->fullName = $doc->name . (!empty($doc->version) ? ' ' . $doc->version : '');
      $documentations[$doc->slug] = $doc;
    }
    return $documentations;
  }

Maybe you need to remove the cache file first.

yannickglt commented 7 years ago

I guess the difference with the current version is that you do not use the cached file after retrieving it. If this existing code does not work, I think it is because the file_put_contents method fails. As I never could reproduce the error, can you confirm that the error comes from the file_put_contents method? I created https://github.com/yannickglt/alfred-devdocs/pull/17 which contains the code you suggested, if you can review it :slightly_smiling_face:

gamelxy commented 7 years ago

OK, this works for me. Maybe @chrisyip can confirm if it works.

chrisyip commented 7 years ago

Unfortunately, it doesn't work for me:

JSON error: JSON text did not start with array or object and option to allow fragments not set. in JSON:
$HOME/Library/Caches/com.runningwithcrayons.Alfred-3/Workflow Data/com.yannickglt.alfred2.devdocs/docs.json0
yannickglt commented 7 years ago

@chrisyip could you upload or send me the docs.json file? I see a trailing 0 at the end of the log you paste, is it a mistake or is it really logged as is?

chrisyip commented 7 years ago

@yannickglt Alfred logged as is.

Here's docs.json file: https://gist.github.com/chrisyip/ea848b57f1f021a7afd1fe62e171233c

yannickglt commented 7 years ago

Thanks. Could you send the index.plist file as well plz?

chrisyip commented 7 years ago

@yannickglt I can't find index.plist file, could you tell me the full path?

yannickglt commented 7 years ago

I merged #17 and bumped a new version of the workflow. Could you have a check on it plz?

chrisyip commented 7 years ago

Works now! But I can't get any result from doc KEYWORD:

screen shot 2017-05-03 at 10 06 07 am screen shot 2017-05-03 at 10 08 37 am
yannickglt commented 7 years ago

Nice! For the doc command, it works on my end. How many docs did you add? It may take time the first time you run the doc command as it will fetch all the added docs. Once the cache is built, it should be instant then.

chrisyip commented 7 years ago

I removed all data and works now! Thank you!

yannickglt commented 7 years ago

You're welcome. Sorry for the delay. I close the issue.

machbio commented 7 years ago

Still get the Same error on Alfred 3.4.1 - should I Open another ticket

Starting debug for 'devdocs'

[2017-08-07 12:43:02][input.scriptfilter] Queuing argument ''
[2017-08-07 12:43:02][input.scriptfilter] Script with argument '(null)' finished
[2017-08-07 12:43:02][input.scriptfilter] Warning: Invalid argument supplied for foreach() in /Users/Username/Library/Application Support/Alfred 3/Alfred.alfredpreferences/workflows/user.workflow.69A0FEC4-E474-47AA-A0CA-D1280C48DDC2/scripts/conf.php on line 97
<?xml version="1.0"?>
<items><item uid="" arg="" valid="no" autocomplete=" "><title>No results.</title><subtitle></subtitle><icon></icon></item></items>
[2017-08-07 12:43:02][ERROR: input.scriptfilter] JSON error: JSON text did not start with array or object and option to allow fragments not set. in JSON:
Warning: Invalid argument supplied for foreach() in /Users/Username/Library/Application Support/Alfred 3/Alfred.alfredpreferences/workflows/user.workflow.69A0FEC4-E474-47AA-A0CA-D1280C48DDC2/scripts/conf.php on line 97
<?xml version="1.0"?>
<items><item uid="" arg="" valid="no" autocomplete=" "><title>No results.</title><subtitle></subtitle><icon></icon></item></items>
yannickglt commented 7 years ago

Could you have a check to the version 1.3.1 of the workflow @machbio?

machbio commented 7 years ago

There seems to be some issue with the Proxy Setting - I have set the HTTP_PROXY in the workflow setting, but it does not work.

[2017-08-07 18:40:02][input.scriptfilter] Queuing argument ''
[2017-08-07 18:40:02][input.scriptfilter] Script with argument '(null)' finished
[2017-08-07 18:40:02][input.scriptfilter] Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known in /Users/Username/Library/Application Support/Alfred 3/Alfred.alfredpreferences/workflows/user.workflow.69A0FEC4-E474-47AA-A0CA-D1280C48DDC2/scripts/devdocs.php on line 73

Warning: file_get_contents(http://maxcdn-docs.devdocs.io/tensorflow~python/index.json): failed to open stream: php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known in /Users/Username/Library/Application Support/Alfred 3/Alfred.alfredpreferences/workflows/user.workflow.69A0FEC4-E474-47AA-A0CA-D1280C48DDC2/scripts/devdocs.php on line 73

Warning: Invalid argument supplied for foreach() in /Users/Username/Library/Application Support/Alfred 3/Alfred.alfredpreferences/workflows/user.workflow.69A0FEC4-E474-47AA-A0CA-D1280C48DDC2/scripts/devdocs.php on line 88
<?xml version="1.0"?>
<items><item uid="No results." arg="http://devdocs.io/tensorflow~python/" valid="yes" autocomplete="No results."><title>No results.</title><subtitle></subtitle><icon>tensorflow~python.png</icon></item></items>
[2017-08-07 18:40:02][ERROR: input.scriptfilter] JSON error: JSON text did not start with array or object and option to allow fragments not set. in JSON:
Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known in /Users/Username/Library/Application Support/Alfred 3/Alfred.alfredpreferences/workflows/user.workflow.69A0FEC4-E474-47AA-A0CA-D1280C48DDC2/scripts/devdocs.php on line 73

Warning: file_get_contents(http://maxcdn-docs.devdocs.io/tensorflow~python/index.json): failed to open stream: php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known in /Users/Username/Library/Application Support/Alfred 3/Alfred.alfredpreferences/workflows/user.workflow.69A0FEC4-E474-47AA-A0CA-D1280C48DDC2/scripts/devdocs.php on line 73

Warning: Invalid argument supplied for foreach() in /Users/Username/Library/Application Support/Alfred 3/Alfred.alfredpreferences/workflows/user.workflow.69A0FEC4-E474-47AA-A0CA-D1280C48DDC2/scripts/devdocs.php on line 88
<?xml version="1.0"?>
<items><item uid="No results." arg="http://devdocs.io/tensorflow~python/" valid="yes" autocomplete="No results."><title>No results.</title><subtitle></subtitle><icon>tensorflow~python.png</icon></item></items>
machbio commented 7 years ago

@yannickglt it seems like your workflow unsets the HTTP_PROXY variable randomly.

yannickglt commented 7 years ago

It might be because of an update of the workflow. I don't know how Alfred handles environment variables when updating worklow. Do you use Packal to automatically update the workflow?

machbio commented 7 years ago

@yannickglt I downloaded the workflow from this github repository..

yannickglt commented 7 years ago

Hi @machbio! The variable erasing should be fixed by fecad49def4a9e6b24cd349fb2d971c775da52c9. You can download v1.3.2 which includes this patch.