Execution error thrown when setting variable to non-existent key in array instead of going into On Error logic

I’m making a curl call to retrieve authentication data from an internal service:

set accessInfo to my curlUrl(curlCmd)

If the user has not authenticated then it returns this array:

{status:"error", message:"Unauthenticated", desc:"You should authenticate (may use ***)", step_up_methods:{{cap_name:" Authentication", cap_guid:"com.****.LDAPSuccess", cap_url:"https://***/login?reauth=1", cap_display_string:"Please login with ***."}}}

When they are authenticated it returns this array which has different key/value pairs:

{accessKeyId:"A***7", assumedRoleId:"***:***@***.COM", expiration:1.656104567E+12, roleARN:"arn:a***:assumed-role/***-DO-NOT-DELETE/***@***.COM", secretAccessKey:"uO*****jrvV", sessionToken:"IQoJb3JpZ2luX2VjEHwaCXVzLXdlc3QtMiJHMEUCIQC8yne7JEsS9k8YdKGtlc3PrapMMpN1NB4ETXVRv0K***"} 

I figured I could use a Try/On Error to set the accessKeyId value and if it was not present it would error, I would know they are unauthenticated, and I would have them authenticate:

	set accessKey to accessKeyId of accessInfo
on error
	run authenticate handler here
end try

But it is throwing an AppleScript Execution Error instead of going into the On Error.

Is there a way around this?

Yes, although it’s not so much a way around, as it is the way to do it. Whenever someone uses try in AppleScript, that is them employing a way around something that will have a more elegant (and robust) method lurking around somewhere.

Regarding records in AppleScript, consider the joining of these two records:

{a : 1, b : "two", d : 4.0} & {b : [2], c : pi, d : 2 ^ 2}

They both have property identifiers in common as well as one each that is unique to them. The resulting record will, of course, contain one copy of each unqiuely named property, so the clue to solving your problem is to observe which value ends up being assigned to the properties of the merged record with identifiers common to both original records?

When you determine that, then to prevent an error from being thrown, jusy ensure that:

set accessKey to accessKeyId of accessInfo

accessInfo definitely has a property identified by accessKeyId, even if it doesn’t come with one originally, whilst also ensuring that, any attempt to insert new properties into the record wouldn’t run the risk of overwriting a pre-existing entry were one to be present.

Thanks. They aren’t two records being returned no merging being done. Found that I could just use a get within the try and it avoids the execution error:

   set accessKey to get accessKeyId of accessInfo
on error
  set message to get message of accessInfo

I know… You were supposed to do some thinking of your own to see how my guidance could be applied to your problem. Anyway, the idea is that you merge the record from which you wish to extract a given property, with a record of your own creation that contains default values for this property:

set accessKey to (accessInfo & {accessKeyId:false})'s accessKeyId

If accessInfo contains the property accessKeyId , then its value will be assigned to your variable; otherwise, the value false is assigned.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.